【问题标题】:Flutter get " type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast errorFlutter get " type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast error
【发布时间】:2019-06-10 10:29:45
【问题描述】:

Out 网络服务器将此结果返回为:

[
  {
    "id": 1,
    "user_id": 10,
    "title": "xxxxxxx",
    "description": "xxxx",
    "type": "post",
    "featured_images": "",
    "slug": "xxxxxxxxx",
    "lang": "xx",
    "visit": 0,
    "click": 0,
    "state": 1,
    "created_at": "xxxxx",
    "updated_at": "xxxxx",
    "categories": [
      {
        "id": 9,
        "title": "xxxx",
        "lang": "fa",
        "parent": 0,
        "click": 0,
        "image": "",
        "created_at": "xxxxx",
        "updated_at": "xxxx",
        "pivot": {
          "contents_id": 1,
          "content_categories_id": 9
        }
      }
    ]
  }
]

我让这个结构类来解析它

part 'contents_model.g.dart';

@JsonSerializable(nullable: false)
class ContentsModel {
  int id;
  @JsonKey(name: 'post_id')
  int postId;
  String title;
  String description;
  String type;
  @JsonKey(name: 'featured_images')
  String featuredImages;
  String slug;
  String lang;
  int visit;
  int click;
  int state;
  @JsonKey(name: 'createdAt')
  String createdAt;
  @JsonKey(name: 'updatedAt')
  String updatedAt;
  @JsonKey(name:'categories')
  CategoriesModel categories;

  ContentsModel(this.id, this.postId, this.title, this.description, this.type, this.featuredImages, this.slug, this.lang, this.visit, this.click, this.state, this.createdAt, this.updatedAt, this.categories);

  factory ContentsModel.fromJson(Map<String,dynamic> json)=>_$ContentsModelFromJson(json);
  Map<String,dynamic> toJson()=>_$ContentsModelToJson(this);
}


part 'categories_model.g.dart';

@JsonSerializable(nullable: true)
class CategoriesModel {
  int id;
  String title;
  String lang;
  int parent;
  int click;
  String image;
  @JsonKey(name: 'created_at')
  String createdAt;
  @JsonKey(name: 'updated_at')
  String updatedAt;

  CategoriesModel(this.id, this.title, this.lang, this.parent, this.click, this.image, this.createdAt, this.updatedAt);

  factory CategoriesModel.fromJson(Map<String,dynamic> json)=>_$CategoriesModelFromJson(json);
  Map<String,dynamic> toJson()=>_$CategoriesModelToJson(this);
}

现在,当我从服务器成功获取数据时,我无法返回该数据,例如:

  Future<List<ContentsModel>> getContents() async {
    final response = await http.get(Constants.getContents);
    if (response.statusCode == 200) {
      final responseString = jsonDecode(response.body);
      try {
             List<ContentsModel> responses = 
                   responseString.map((j) =>
                        ContentsModel.fromJson(j)).toList();
             return responses;
      }catch(error){
        print('Error:  $error');
        return null;
      }
    } else {
      throw Exception('error fetche contents');
    }
  }

对于这行代码:

return responseString.map&lt;List&lt;ContentsModel&gt;&gt;((json) =&gt; ContentsModel.fromJson(json)).toList();

我得到这个错误:

类型“列表”不是类型“地图”的子类型 在类型转换中

【问题讨论】:

  • 您的CategoriesModel 似乎没有考虑到接收List(请注意在json 中围绕它的[])。您必须考虑在类别中接收多个类别。

标签: flutter dart


【解决方案1】:

@Richard Heap 评论,更改

CategoriesModel categories;

List<CategoriesModel> categories;

解决了这个问题

【讨论】:

  • 您好,您可以将此答案标记为已接受吗?
  • @croxx5f 是的,我做到了
猜你喜欢
  • 1970-01-01
  • 2020-04-12
  • 2023-02-17
  • 2021-09-08
  • 2021-10-22
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 2023-01-12
相关资源
最近更新 更多