【发布时间】:2020-04-28 07:49:22
【问题描述】:
谁能解释一下为什么这段代码不起作用?
我有两节课
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.fromJson 失败并出现以下错误: “IngredientEntry”类没有名为“fromJson”的构造函数。 尝试调用不同的构造函数,或者定义一个名为 'fromJson'.dart(new_with_undefined_constructor) 的构造函数
我不明白我做错了什么。 感谢您的帮助!
【问题讨论】:
-
你找到解决办法了吗?
标签: json flutter constructor