Fix bug preventing addind and editing meal items

This commit is contained in:
Roland Geider
2021-04-28 11:47:30 +02:00
parent 0673cbf3ce
commit 4c7452e634
2 changed files with 6 additions and 5 deletions

View File

@@ -67,8 +67,10 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
Meal? findMealById(int id) {
for (var plan in _plans) {
var meal = plan.meals.firstWhere((plan) => plan.id == id);
return meal;
try {
var meal = plan.meals.firstWhere((plan) => plan.id == id);
return meal;
} on StateError catch (e) {}
}
return null;
}
@@ -201,8 +203,7 @@ class Nutrition extends WgerBaseProvider with ChangeNotifier {
}
/// Adds a meal item to a meal
Future<MealItem> addMealItem(MealItem mealItem, int mealId) async {
var meal = findMealById(mealId)!;
Future<MealItem> addMealItem(MealItem mealItem, Meal meal) async {
final data = await post(mealItem.toJson(), makeUrl(_mealItemPath));
mealItem = MealItem.fromJson(data);

View File

@@ -177,7 +177,7 @@ class MealItemForm extends StatelessWidget {
_form.currentState!.save();
try {
Provider.of<Nutrition>(context, listen: false).addMealItem(_mealItem, _meal.id!);
Provider.of<Nutrition>(context, listen: false).addMealItem(_mealItem, _meal);
} on WgerHttpException catch (error) {
showHttpExceptionErrorDialog(error, context);
} catch (error) {