Flutter JSON 该类没有名为“fromJson”的构造函数

Posted

技术标签:

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

我不明白我做错了什么。 感谢您的帮助!

【问题讨论】:

你找到解决办法了吗? 【参考方案1】:

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

【讨论】:

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

如果有人提出这个问题,

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

【讨论】:

以上是关于Flutter JSON 该类没有名为“fromJson”的构造函数的主要内容,如果未能解决你的问题,请参考以下文章

在 Flutter 中对 JSON 列表进行排序 [重复]

Flutter 错误:没有名为“keyboardType”的命名参数。键盘类型:TextInputType.text

类没有名为的成员

QT软件开发: 解析JSON数据(QJsonX)

没有为该类定义 Getter。我在这里做错了啥?

有没有什么好方法可以在Flutter中创建一个POST请求到JSON?