Может кто-нибудь объяснить мне, почему этот код работает?
У меня есть два класса
IngredientEntry.dart
import 'package:CoolKiosk/MasterData/ingredient.dart';
import 'package:decimal/decimal.dart';
class IngredientEntry {
Decimal addOnPrice;
Ingredient ingredient;
IngredientEntry(Decimal addOnPrice, Ingredient ingredient) {
this.addOnPrice = addOnPrice;
this.ingredient = ingredient;
}
factory IngredientEntry.fromJson(Map<String, dynamic> json) {
var _addOnPrice = Decimal.parse(json['price'].toString());
var _ingredient = Ingredient.fromJson(json['ingredient']);
return new IngredientEntry(_addOnPrice, _ingredient);
}
}
и класс IngredientGroup.dart
import 'package:CoolKiosk/CustomWidget/IngredientEntry.dart';
import 'package:CoolKiosk/MasterData/base_object.dart';
class IngredientGroup extends BaseObject {
String networkId;
String satelliteId;
String name;
int minSelection;
int maxSelection;
int includedItems;
List<IngredientEntry> ingredientEntries = new List<IngredientEntry>();
IngredientGroup(String id, String networkId, String satelliteId, String name,
int minSel, int maxSel, int includedItems)
: super(id) {
this.networkId = networkId;
this.satelliteId = satelliteId;
this.name = name;
this.minSelection = minSel;
this.maxSelection = maxSel;
this.includedItems = includedItems;
}
factory IngredientGroup.fromJson(Map<String, dynamic> json) {
var _id = json['_id'];
var _networkId = json['networkId'];
var _satelliteId = json['satelliteId'];
var _name = json['name'];
var _minSelection = json['minSelection'];
var _maxSelection = json['maxSelection'];
var _includedItems = json['includedItems'];
IngredientGroup ingredientGroup = new IngredientGroup(_id, _networkId,
_satelliteId, _name, _minSelection, _maxSelection, _includedItems);
final _ingredientEntryList = json['ingredientEntries'];
for (var i = 0; i < _ingredientEntryList.length; i++) {
ingredientGroup.ingredientEntries
.add(new IngredientEntry.fromJson(_ingredientEntryList[i]));
}
return ingredientGroup;
}
}
class Ingredient.dart
import 'package:CoolKiosk/MasterData/base_object.dart';
import 'package:decimal/decimal.dart';
class Ingredient extends BaseObject {
String networkId;
String satelliteId;
String name;
Decimal addOnPrice;
Decimal downPrice;
Ingredient(String id, String networkId, String satelliteId, String name,
Decimal addOnPrice, Decimal downPrice)
: super(id) {
this.networkId = networkId;
this.satelliteId = satelliteId;
this.name = name;
this.addOnPrice = addOnPrice;
this.downPrice = downPrice;
}
factory Ingredient.fromJson(Map<String, dynamic> json) {
var _id = json['_id'];
var _networkId = json['networkId'];
var _satelliteId = json['satelliteId'];
var _name = json['name'];
var _addOnPrice = Decimal.parse(json['addOnPrice'].toString());
var _downPrice = Decimal.parse(json['downPrice'].toString());
return Ingredient(
_id, _networkId, _satelliteId, _name, _addOnPrice, _downPrice);
}
}
Деталь IngredientEntry.from Json завершается ошибкой: Класс IngredientEntry не имеет конструктора с именем from 1020 *». Попробуйте вызвать другой конструктор или определить конструктор с именем «from Json». Dart (new_with_undefined_constructor)
Я не понимаю, что я делаю неправильно. Спасибо за любую помощь!