当前位置: 首页 > 知识库问答 >
问题:

颤振获取“类型”列表“不是类型转换错误中“Map”类型的子类型

易祖鹤
2023-03-14

out webserver将此结果返回为:

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

对于这行代码

返回tring.map

我得到这个错误:

类型列表不是类型转换中的类型映射的子类型

共有1个答案

周凯捷
2023-03-14

作为@Richard Heap注释,正在更改

CategoriesModel categories;

List<CategoriesModel> categories;

解决了这个问题

 类似资料:
  • 我的JSON有点像这样: 我的模型类: 这是我的api调用: 如何获取图像。小班?如果我需要澄清我的问题,请告诉我。我得到的错误列表不是Map类型的子类型

  • 我正面临着一个奇怪的颤动错误。我使用json序列化。 这是我的密码 我的web api发送的数据如下 这是数组的数组。 生成错误的代码是 它给出的错误 JSON数据这里是JSON数据格式的屏幕截图

  • 当我尝试解码json时,它会给我错误。 我的模范班 功能在我使用它的地方 json数据

  • 如果有人知道如何修复这个错误,我会非常感激!我只是想使用API访问网站,saleTitle和saleCode然后显示它。所有的代码都在下面,希望这只是一个我忽略的简单修复 任何想要查看数据结构的人都可以使用API 或者这里的Json数据 模型 屏幕文件

  • 我目前正在构建一个应用程序,通过医疗api读取数据,我试图解析JSON。 我为用户创建了一个模型类: 这是我试图阅读的JSON信息: 但是我得到的错误列表不是Map类型的子类型 如何修复此错误?

  • 我是颤振开发方面的新手。我犯了一个错误 列表不是“Iterable”类型的子类型 到目前为止,我已经做到了。 模范班 缅因州班内 } 我试图从2天开始解决这个问题。 请帮忙,谢谢。 这里是JSON响应,您可以检查链接