Reputation: 91
I want to update my image in Firebase storage when editing a recipe. But the storagePath.delete() method does not work. It throws the following error:
[firebase_storage/object-not-found] No object exists at the desired reference)
Is there a solution to this problem, or maybe just another way to replace an image that I don't know about?
Map<String, dynamic> initValues = {
'title': '',
'description': '',
'ingredients': [''],
'steps': [''],
'imageUrl': '',
'creatorId': '',
'p': '',
'c': '',
'f': '',
'kcal': '',
'servings': '',
'id': '',
};
@override
void didChangeDependencies() {
final String? recipeId =
ModalRoute.of(context)!.settings.arguments as String?;
if (recipeId != null) {
newRecipe = functions.findById(recipeId);
initValues = {
'title': newRecipe.title,
'description': newRecipe.description,
'ingredients': newRecipe.ingredients,
'steps': newRecipe.steps,
'imageUrl': newRecipe.imageUrl,
'creatorId': newRecipe.creatorId,
'p': newRecipe.p.toString(),
'c': newRecipe.c.toString(),
'f': newRecipe.f.toString(),
'kcal': newRecipe.kcal.toString(),
'servings': newRecipe.servings.toString(),
'id': recipeId,
};
newRecipe.id = recipeId;
}
super.didChangeDependencies();
}
@override
void dispose() {
_descriptionNode.dispose();
_ingredientsNode.dispose();
_instructionsNode.dispose();
super.dispose();
}
Future<void> saveData() async {
bool isValid = _formKey.currentState!.validate();
setState(() {
isLoading = true;
});
if (isValid == true) {
_formKey.currentState!.save();
final storagePath = FirebaseStorage.instance
.ref()
.child('recipe_image')
.child(newRecipe.id + '.jpg');
if (initValues['id'] != '') {
storagePath.delete();
}
await storagePath.putFile(pickedImage);
final imageUrl = await storagePath.getDownloadURL();
newRecipe.imageUrl = imageUrl;
Navigator.of(context).pushNamed(NutritionScreen.routeName, arguments: {
'title': newRecipe.title,
'description': newRecipe.description,
'ingredients': newRecipe.ingredients,
'steps': newRecipe.steps,
'imageUrl': newRecipe.imageUrl,
'creatorId': newRecipe.creatorId,
'p': newRecipe.p,
'c': newRecipe.c,
'f': newRecipe.f,
'kcal': newRecipe.kcal,
'servings': newRecipe.servings,
'id': newRecipe.id,
});
}
setState(() {
isLoading = false;
});
}
Future<void> editRecipe(Recipe newRecipe) async {
final recipeIndex =
_recipes.indexWhere((element) => element.id == newRecipe.id);
if (recipeIndex >= 0) {
final url = Uri.parse(
'https://recipier-e1139-default-rtdb.europe-west1.firebasedatabase.app/recipes/${newRecipe.id}.json');
try {
await http.patch(
url,
body: json.encode(
{
'title': newRecipe.title,
'description': newRecipe.description,
'ingredients': newRecipe.ingredients,
'steps': newRecipe.steps,
'kcal': newRecipe.kcal,
'p': newRecipe.p,
'c': newRecipe.c,
'f': newRecipe.f,
'servings': newRecipe.servings,
'imageUrl': newRecipe.imageUrl,
'creatorId': userId,
},
),
);
_recipes[recipeIndex] = newRecipe;
} catch (error) {
rethrow;
}
}
notifyListeners();
}
Upvotes: 0
Views: 291
Reputation: 87
Try to check your problem with debugger https://docs.flutter.dev/development/tools/devtools/debugger Check how your editRecipe function works.
Upvotes: 1