mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-02 18:19:25 +01:00
Fix error when that prevented adding ingredients to a meal
This commit is contained in:
@@ -46,7 +46,7 @@ class Log {
|
||||
late Ingredient ingredientObj;
|
||||
|
||||
@JsonKey(required: true, name: 'weight_unit')
|
||||
late int weightUnitId;
|
||||
int? weightUnitId;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
IngredientWeightUnit? weightUnitObj;
|
||||
@@ -81,9 +81,9 @@ class Log {
|
||||
// This is already done on the server. It might be better to read it from there.
|
||||
var out = NutritionalValues();
|
||||
|
||||
final weight = amount;
|
||||
// final weight =
|
||||
// this.weightUnitObj == null ? amount : amount * weightUnitObj.amount * weightUnitObj.grams;
|
||||
//final weight = amount;
|
||||
final weight =
|
||||
this.weightUnitObj == null ? amount : amount * weightUnitObj!.amount * weightUnitObj!.grams;
|
||||
|
||||
out.energy = ingredientObj.energy * weight / 100;
|
||||
out.protein = ingredientObj.protein * weight / 100;
|
||||
|
||||
@@ -18,7 +18,7 @@ Log _$LogFromJson(Map<String, dynamic> json) {
|
||||
return Log(
|
||||
id: json['id'] as int?,
|
||||
ingredientId: json['ingredient'] as int,
|
||||
weightUnitId: json['weight_unit'] as int,
|
||||
weightUnitId: json['weight_unit'] as int?,
|
||||
amount: toNum(json['amount'] as String?),
|
||||
planId: json['plan'] as int,
|
||||
datetime: DateTime.parse(json['datetime'] as String),
|
||||
|
||||
@@ -30,29 +30,29 @@ class MealItem {
|
||||
@JsonKey(required: true)
|
||||
int? id;
|
||||
|
||||
@JsonKey(required: false)
|
||||
@JsonKey(required: false, name: 'meal')
|
||||
late int mealId;
|
||||
|
||||
@JsonKey(required: false, name: 'ingredient')
|
||||
int ingredientId;
|
||||
late int ingredientId;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
late Ingredient ingredientObj;
|
||||
|
||||
@JsonKey(required: false, name: 'weight_unit')
|
||||
int weightUnitId;
|
||||
int? weightUnitId;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
late IngredientWeightUnit weightUnitObj;
|
||||
IngredientWeightUnit? weightUnitObj;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
num amount;
|
||||
late num amount;
|
||||
|
||||
MealItem({
|
||||
this.id,
|
||||
int? mealId,
|
||||
required this.ingredientId,
|
||||
required this.weightUnitId,
|
||||
this.weightUnitId,
|
||||
required this.amount,
|
||||
}) {
|
||||
if (mealId != null) {
|
||||
@@ -60,6 +60,8 @@ class MealItem {
|
||||
}
|
||||
}
|
||||
|
||||
MealItem.empty();
|
||||
|
||||
// Boilerplate
|
||||
factory MealItem.fromJson(Map<String, dynamic> json) => _$MealItemFromJson(json);
|
||||
|
||||
|
||||
@@ -10,16 +10,16 @@ MealItem _$MealItemFromJson(Map<String, dynamic> json) {
|
||||
$checkKeys(json, requiredKeys: const ['id', 'amount']);
|
||||
return MealItem(
|
||||
id: json['id'] as int?,
|
||||
mealId: json['mealId'] as int?,
|
||||
mealId: json['meal'] as int?,
|
||||
ingredientId: json['ingredient'] as int,
|
||||
weightUnitId: json['weight_unit'] as int,
|
||||
weightUnitId: json['weight_unit'] as int?,
|
||||
amount: toNum(json['amount'] as String?),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> _$MealItemToJson(MealItem instance) => <String, dynamic>{
|
||||
'id': instance.id,
|
||||
'mealId': instance.mealId,
|
||||
'meal': instance.mealId,
|
||||
'ingredient': instance.ingredientId,
|
||||
'weight_unit': instance.weightUnitId,
|
||||
'amount': toString(instance.amount),
|
||||
|
||||
@@ -100,8 +100,8 @@ class MealItemForm extends StatelessWidget {
|
||||
late MealItem _mealItem;
|
||||
|
||||
MealItemForm(this._meal, [mealItem]) {
|
||||
this._mealItem = mealItem;
|
||||
//this._mealItem = mealItem ?? MealItem(mealId: _meal.id);
|
||||
this._mealItem = mealItem ?? MealItem.empty();
|
||||
_mealItem.mealId = _meal.id!;
|
||||
}
|
||||
|
||||
final _form = GlobalKey<FormState>();
|
||||
@@ -142,14 +142,11 @@ class MealItemForm extends StatelessWidget {
|
||||
if (value!.isEmpty) {
|
||||
return AppLocalizations.of(context)!.selectIngredient;
|
||||
}
|
||||
if (_mealItem.ingredientId == null) {
|
||||
return AppLocalizations.of(context)!.selectIngredient;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
TextFormField(
|
||||
decoration: InputDecoration(labelText: AppLocalizations.of(context)!.amount),
|
||||
decoration: InputDecoration(labelText: AppLocalizations.of(context)!.weight),
|
||||
controller: _amountController,
|
||||
keyboardType: TextInputType.number,
|
||||
onFieldSubmitted: (_) {},
|
||||
|
||||
@@ -100,7 +100,7 @@ class MealItemWidget extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
String unit = _item.weightUnitId == null
|
||||
? AppLocalizations.of(context)!.g
|
||||
: _item.weightUnitObj.weightUnit.name;
|
||||
: _item.weightUnitObj!.weightUnit.name;
|
||||
final values = _item.nutritionalValues;
|
||||
|
||||
return Container(
|
||||
|
||||
Reference in New Issue
Block a user