类型“_InternalLinkedHashMap<dynamic, dynamic>”不是“函数结果”的“List<Map<String, dynamic>>”类

Posted

技术标签:

【中文标题】类型“_InternalLinkedHashMap<dynamic, dynamic>”不是“函数结果”的“List<Map<String, dynamic>>”类型的子类型【英文标题】:type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' of 'function result' 【发布时间】:2021-04-14 13:06:30 【问题描述】:

在写这个问题之前,我已经查看了以下链接:

Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> Dart - how _InternalLinkedHashMap<String, dynamic> convert to Map<String, dynamic>? https://medium.com/codespace69/flutter-json-decode-type-internallinkedhashmap-dynamic-dynamic-is-not-a-subtype-of-type-9d6b3e982b59

代码:

List data = [
    
      "title" : "Particulate matter",
      "desc" : "The term used to describe a mixture of solid particles and liquid droplets found in the air",
      "amt" : 500,
      "diseases" : "Particulate matter is responsible for asthma in many people. Also, a topic dermatitis, allergic rhinitisare diseases that can be caused by this",
      "precaution" : "Switching to cleaner appliances and reducing the amount of smoking will surely ensure less exposureof particulate matter in the environment",
      "image" : 'https://images.pexels.com/photos/3668372/pexels-photo-3668372.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
      "color" : Color(0xFF6E7E5D).withOpacity(.3)
    ,
// loads of data in this structure
];

// the line of error
Map indexedData = Map<String, dynamic>.from(data[index]);

我只是不知道为什么会出现错误,所以请帮助我。谢谢!

编辑:如果这有助于解决问题,我可以将数据更改为限制

【问题讨论】:

实际代码而不是损坏的元代码怎么样? 我已经用实际数据更新了,请查收 索引中有什么?我希望为零。 它实际上在ListView.builder 下,如果你愿意,我可以将其添加到问题中 该错误指向哪一行?你展示的任何东西似乎都不可能。 【参考方案1】:

你应该像这样声明类型:

Map<String, dynamic> indexedData = Map<String, dynamic>.from(data[index]);

【讨论】:

【参考方案2】:
List data = [
    
      "title": "Particulate matter",
      "desc":
          "The term used to describe a mixture of solid particles and liquid droplets found in the air",
      "amt": 500,
      "diseases":
          "Particulate matter is responsible for asthma in many people. Also, a topic dermatitis, allergic rhinitisare diseases that can be caused by this",
      "precaution":
          "Switching to cleaner appliances and reducing the amount of smoking will surely ensure less exposureof particulate matter in the environment",
      "image":
          'https://images.pexels.com/photos/3668372/pexels-photo-3668372.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260',
      "color": Color(0xFF6E7E5D).withOpacity(.3),
    ,
  ];

  Map<String, dynamic> indexedData = ;

  data.forEach((mapElement) 
    Map map = mapElement as Map;
    map.forEach((key, value) 
      indexedData[key] = value;
    );
  );

print(indexedData);

【讨论】:

以上是关于类型“_InternalLinkedHashMap<dynamic, dynamic>”不是“函数结果”的“List<Map<String, dynamic>>”类的主要内容,如果未能解决你的问题,请参考以下文章

新:类型“_InternalLinkedHashMap<String, dynamic>”不是“DocumentSnapshot”类型的子类型

获取 _InternalLinkedHashMap<String, dynamic>' 不是 'FutureOr<Response>' 类型的子类型

_CastError(类型 '_InternalLinkedHashMap<dynamic, dynamic>' 不是类型转换中类型 'Map<String, dynamic>

_TypeError(类型 '_InternalLinkedHashMap<String, dynamic>' 不是类型 'Iterable<dynamic>' 的子类型) F

type '_InternalLinkedHashMap<String, dynamic>' 不是类型 'Iterable<dynamic>' 的子类型

破坏异常 '_InternalLinkedHashMap<dynamic, dynamic>' 不是类型 'Map<String, String>' 的子类型