【问题标题】:Flutter: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> in type castFlutter:InternalLinkedHashMap<String, dynamic>' 不是类型转换中“List<dynamic>”类型的子类型
【发布时间】:2023-03-25 13:34:01
【问题描述】:

我正在尝试从 api 获取一些数据,但它一直给我这个错误。其他数据模型也发生了同样的错误,但它可以自行运行,但这次没有。

我的数据模型:

class StoresModel {
final id;
final url;
final List<StoreModel> store;

StoresModel(this.id,this.url,this.store);

StoresModel.fromJson(Map<String, dynamic> json)
  : id=json["id"]??"",
    url=json["url"]??"",
    store=(json["store"]as 
  List)?.map((i)=>StoreModel.fromJson(i))?.toList() 
 ?? []; // Here's the error !!

} 
 class StoreModel {
 final id;
 final name;

StoreModel(this.id,this.name);

StoreModel.fromJson(Map<String, dynamic> json)
  : id=json["id"]??"",
    name=json["name"]??"";
}

API:

"stores": [
{

"id": 290375,
"url": "https://store.playstation.com/en-us/product/UP1004-CUSA00419_00-GTAVDIGITALDOWNL",
"store": {
"id": 3,
"name": "PlayStation Store",
"slug": "playstation-store",
"domain": "store.playstation.com",
"games_count": 7377,
"image_background": 
"https://media.rawg.io/media/games/328/3283617cb7d75d67257fc58339188742.jpg"
 }
 },

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    根据输出 API,store 是 Object,而不是 List。 所以应该是

    final StoreModel store;
    

    你必须将 fromJson 方法更改为 store

    store = json['store'] == null
            ? null
            : StoreModel.fromJson(json['store'] as Map<String, dynamic>)
    

    【讨论】:

    • 它向我显示了这个错误:未处理的异常:类型“(动态)=> StoreModel”不是“转换”类型“(字符串,动态)=> MapEntry”的子类型'
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 2020-12-21
    • 1970-01-01
    • 2021-12-29
    • 2023-01-08
    • 2019-09-19
    • 2021-10-25
    • 2020-07-03
    相关资源
    最近更新 更多