【问题标题】:type '_InternalLinkedHashMap<Object?, Object?>' is not a subtype of type 'Map<String, dynamic>' when parsing Firebase response解析 Firebase 响应时,类型 \'_InternalLinkedHashMap<Object?, Object?>\' 不是类型 \'Map<String, dynamic>\' 的子类型
【发布时间】:2022-09-27 14:30:13
【问题描述】:

我的解析代码是这样的。

  class Model {
      List<Info>? info,
      idx,
      name,
      image,
      kcal,
      effect,
      keep,
      tip,
      youtube01,
      youtube02,
      youtube03,
      season;

      Model(
      {this.info,
      this.idx,
      this.name,
      this.image,
      this.kcal,
      this.effect,
      this.keep,
      this.tip,
      this.youtube01,
      this.youtube02,
      this.youtube03,
      this.season});

      Model.fromJson(Map<String, dynamic> json)
      : idx = json[\'idx\'],
        name = json[\'name\'],
        image = json[\'image\'],
        kcal = json[\'kcal\'],
        info = json[\'info\'],
        effect = json[\'effect\'],
        keep = json[\'keep\'],
        tip = json[\'tip\'],
        youtube01 = json[\'youtube01\'],
        youtube02 = json[\'youtube02\'],
        youtube03 = json[\'youtube03\'],
        season = json[\'season\'];

        Map<String, dynamic> toJson() => {
        \'idx\': idx,
        \'name\': name,
        \'image\': image,
        \'kcal\': kcal,
        \'info\': info,
        \'effect\': effect,
        \'keep\': keep,
        \'tip\': tip,
        \'youtube01\': youtube01,
        \'youtube02\': youtube02,
        \'youtube03\': youtube03,
              \'season\': season,
           };
     }

这就是正文的代码结构。

  void _dataBase() async {
    var ref = FirebaseDatabase.instance
        .ref(\"Info/\")
        .orderByChild(\"season\")
        .equalTo(\"january04\");
    ref.onValue.listen((DatabaseEvent event) {
      dataJson = event.snapshot.value;
      //var jsonResult = jsonDecode(dataJson);
      var models = Model.fromJson(dataJson);
      //print(dataJson);
      print(models);
    });
  }

但我在控制台中收到此错误。

\"未处理的异常:类型 \'_InternalLinkedHashMap<Object?, Object?>\' 不是类型 \'Map<String, dynamic>\'\ 的子类型"

jsonparsing方法错了吗?无论你如何使用 jsonDecode 或任何方法,都会出现错误。 请帮我怎么做。感谢你。

    标签: json flutter dart


    【解决方案1】:

    您可以使用new Map&lt;String, dynamic&gt;.from(event.snapshot.value);

    【讨论】:

      【解决方案2】:

      第一次检查响应是否为空然后执行此操作:

       Map<dynamic, dynamic> toJson() => { //Change here 
              'idx': idx,
              'name': name,
              'image': image,
              'kcal': kcal,
              'info': info,
              'effect': effect,
              'keep': keep,
              'tip': tip,
              'youtube01': youtube01,
              'youtube02': youtube02,
              'youtube03': youtube03,
                    'season': season,
                 };
      

      【讨论】:

        猜你喜欢
        • 2022-12-23
        • 2019-09-21
        • 2021-07-10
        • 2018-12-14
        • 2023-04-02
        • 2019-04-03
        • 2021-10-06
        • 2019-05-02
        • 1970-01-01
        相关资源
        最近更新 更多