【问题标题】:Type Mismatch during json parsing in flutter颤振中json解析期间的类型不匹配
【发布时间】:2018-07-24 05:43:35
【问题描述】:

我有一个作为列表返回的 json。我正在尝试解析它,但我收到一个名为

的错误

_InternalLinkedHashMap' 不是“List”类型的子类型 这是我的代码 -

 List data = json.decode(response.body) as List; 
    var newsPageViewResult = data.map((i) => new NewsList.fromJson(i)).toList();

和我的 json 模型类

    class NewsList {
  final List<News> news;

  NewsList({
    this.news,
  });

  factory NewsList.fromJson(List<dynamic> parsedJson) {

    List<News> news = new List<News>();
    news = parsedJson.map((i)=>News.fromJson(i)).toList();

    return new NewsList(
        news: news
    );
  }
}

class News{
  final String status;
  final String type;

  News({
    this.status,
    this.type
  }) ;

  factory News.fromJson(Map<String, dynamic> json){
    return new News(

      status: json['status'],
      type: json['type'],
    );
  }}  

                                                                                                                                                                also this is how json is returning                                                                                                                    

[ { “身份证”:10159, “日期”:“2018-07-23T11:34:22”, "date_gmt": "2018-07-23T11:34:22", “指导”:{ “渲染”:“http://the2is.com/?p=10159” }, “修改”:“2018-07-23T11:35:13”, “modified_gmt”:“2018-07-23T11:35:13”, “蛞蝓”:“只有 25 卢比将在儿童缺血时被移除”, “状态”:“发布”, “类型”:“发布”,....]

我已经根据 Dart 2.0 文档明确声明了传递什么类型的数据,但仍然存在错误。

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    首先,删除var,它只会降低可读性。
    据我所知,您收到一个 json 格式的新闻列表,然后将其映射为 List,然后您想要遍历此列表并创建一个 NewsLists 列表。

    相反,您应该按照您的设计从列表中创建一个NewsList 对象:)

    List data = json.decode(response.body) as List; 
    NewsList newsPageViewResult = new NewsList.fromJson(data)
    

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 2021-10-27
      • 2023-03-05
      • 2019-04-05
      • 1970-01-01
      • 2021-08-23
      • 2020-03-01
      • 2021-09-25
      • 2021-02-25
      相关资源
      最近更新 更多