颤振得到“输入“列表<动态>”不是'Map< String,dynamic>'类型的子类型类型转换错误 [英] Flutter get " type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in type cast error

查看:109
本文介绍了颤振得到“输入“列表<动态>”不是'Map< String,dynamic>'类型的子类型类型转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络服务器返回的结果为:

Out webserver return this result as:

[
  {
    "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
        }
      }
    ]
  }
]

并且我使该结构类对其进行解析

and i make this structure class to parse it

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);
}

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

now when i get data from server successful i can't return that, for example:

  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');
    }
  }

此行代码:

返回responseString.map< List< ContentsModel>>>(((json)=> ContentsModel.fromJson(json))。toList();

我收到此错误:


类型列表为不是类型'map'的子类型
在类型转换中

type 'List' is not a subtype of type 'Map' in type cast


推荐答案

为< a href = https://stackoverflow.com/users/9597706/richard-heap> @理查德堆评论,更改

CategoriesModel categories;

List<CategoriesModel> categories;

解决此问题

这篇关于颤振得到“输入“列表&lt;动态&gt;”不是'Map&lt; String,dynamic&gt;'类型的子类型类型转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆