【问题标题】:Flutter Exception on JSONJSON 上的颤振异常
【发布时间】:2022-01-17 16:09:54
【问题描述】:

错误:类型 '(dynamic) => Null' 不是类型 '(String, dynamic) => void'的子类型

大家好,我是 Flutter 的新手,我从 Firebase 存储中获取数据并传递给我的模型,但是发生了这个错误!所以你能解释一下为什么会发生这种情况以及我该如何解决这个问题,

MainModel.fromJson(dynamic json) {
    if (json['tax_center'] != null) {
      print(json['tax_center']); //{address: New York, name: centeral}
      _taxCenter = [];
      json['tax_center'].forEach((v) {
        _taxCenter?.add(Tax_center.fromJson(v));
      });
    }
...

感谢您的宝贵时间!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    通过打印我可以看到 json['tax_center'] 返回 Map 而不是列表。

    使用 forEach 您将其视为一个列表。

    尝试做这样的事情:

    if (json['tax_center'] != null) {
          print(json['tax_center']); //{address: New York, name: centeral}
          _taxCenter = [];
          _taxCenter?.add(Tax_center.fromJson(json['tax_center']));
    }
    

    【讨论】:

      【解决方案2】:
      MainModel.fromJson(json) { //don't use key type
          if (json['tax_center'] != null) {
            print(json['tax_center']); //{address: New York, name: centeral}
            _taxCenter = [];
            json['tax_center'].forEach((v) {
              _taxCenter?.add(Tax_center.fromJson(v));
            });
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-25
        • 2019-11-27
        • 2020-12-05
        • 2022-01-05
        • 2019-10-05
        • 2020-08-27
        • 2021-06-18
        • 1970-01-01
        相关资源
        最近更新 更多