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

时间:2020-09-15 17:01:30

标签: json list flutter

我遇到此错误,并且看到了更多主题,但我无法解决它。

我的模特:

class JobModel {
final String title;
final int id;
final String slug;
final String content;

JobModel(this.title, this.id, this.slug, this.content);

这就是我尝试获取数据的方式

Future<List<JobModel>> _getJobs() async {
var response = await http.get(
    "...?pathname=/anunturi/pagina-1&userID=");
Map<String, dynamic> map = json.decode(response.body);
List<dynamic> data = map["context"];
return data;
}

我在此行List< dynamic > data = map["context"]

上收到错误

打印(地图):

{context: {categories: [{id: 15, title: Auto-Moto, slug: auto-moto, content: Vanzari-cumparari de automobile, piese acte. Anunţurile sunt destinate zonei Radauti-Suceava<br />, related: {link: /anunturi/auto-moto-15, archiveLink: /arhiva-anunturi/c/auto-moto-15, deletedPostCount: 387, postCount: 22}}, {id: 16, title: Diverse, slug: diverse, content: , related: {link: /anunturi/diverse-16, archiveLink: /arhiva-anunturi/c/diverse-16, deletedPostCount: 189, postCount: 18}}, {id: 17, title: Electronice/electrocasnice, slug: electronice-electrocasnice, content: Vanzari, cumparari, inchirieri de aparate electronice şi electrocasnice. Calculatoare, camere digitale, componente, accesorii, telefoane GSM, camere video, playere DVD, etc.
I/flutter (11118): , related: {link: /anunturi/electronice-electrocasnice-17, archiveLink: /arhiva-anunturi/c/electronice-electrocasnice-17, deletedPostCount: 70, postCount: 3}}, {id: 19, title: Imobiliare, slug: imobiliare, content: <p>Vanzari, cumparari şi &icirc;nchirieri de apartamente, case, ter

1 个答案:

答案 0 :(得分:1)

您的Map<String, dynamic> map对象的类型为Map<String, dynamic>,而不是List<JobModel>。您将需要将要使用的地图转换为要查找的List<JobModel>

我假设在响应对象下有一个字段context,它直接以列表的形式映射到JobModel,并且是这样的字段:

{
  "context": [
    // first JobModel Map
    {
      "title": "some title here",
      "id": 1234,
      "slug": "some slug value",
      "content": "the rest of the object content",
    },
    // second JobModel Map
    {
      "title": "some other title here",
      "id": 5678,
      "slug": "some other slug value",
      "content": "blah blah,",
    },
  ]
}

您的班级/ job_model.dart应该看起来像这样。

class JobModel {
  final String title;
  final int id;
  final String slug;
  final String content;

  JobModel(this.title, this.id, this.slug, this.content);

  // for converting your object into a json object
  Map<String, dynamic> toJson() => {
        'title': title,
        'id': id,
        'slug': slug,
        'content': content,
      };

  // for init from a json object.
  JobModel.fromJson(Map<String, dynamic> json)
      : title = json['title'],
        id = json['id'],
        slug = json['slug'],
        content = json['content'];
}

,然后在进行提取时,应像这样使用它:

Future<List<JobModel>> _getJobs() async {
  // Looks like the example response above I gave
  var response = await http.get(
      "...?pathname=/anunturi/pagina-1&userID=");
  
  Map<String, dynamic> jsonResponse = json.decode(response.body);
  List<JobModel> jobList = List<JobModel>();
  jsonResponse.forEach((jsonJob) {
    // using the constructor we made earlier
    JobModel job = JobModel.fromJson(jsonJob);
    jobList.add(job);
  });
  return jobList;
}

您应该提供有关响应本身的更多信息,以便为您的案件提供更好的解决方案。