【问题标题】:Flutter JSON The class doesn't have a constructor named 'fromJson'Flutter JSON 该类没有名为“fromJson”的构造函数
【发布时间】: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


【解决方案1】:

也许您的成分类没有工厂方法。您也可以将其编辑到问题中吗?

【讨论】:

  • 我编辑了这个问题 - 我认为成分类应该没问题
【解决方案2】:

如果有人提出这个问题,

coude aboce 应该可以正常运行。我的错误是在这个片段之外,我有一个类和一个名为 IngredientEntry 的小部件。

【讨论】:

    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 2022-01-24
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    相关资源
    最近更新 更多