【问题标题】:_TypeError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>') [closed]_TypeError(类型'_InternalLinkedHashMap<String,dynamic>'不是'List<dynamic>'类型的子类型)[关闭]
【发布时间】:2021-01-02 11:53:40
【问题描述】:

我有这个错误,我看到了更多主题,但我无法解决它。

我的模型:

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&lt; dynamic &gt; 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

【问题讨论】:

  • 预期响应的示例是什么?
  • 当我尝试使用 DIO 并且没有更改变量名时得到的响应
  • 请至少发布预期 JSON 的结构,或者只是 print(map);
  • 我会在 10 分钟内发布打印(地图)

标签: json list flutter


【解决方案1】:

您的 Map&lt;String, dynamic&gt; map 对象的类型为 Map&lt;String, dynamic&gt; 而不是 List&lt;JobModel&gt;。您需要将您正在使用的地图转换为您正在寻找的List&lt;JobModel&gt;

我假设在响应对象下您有一个字段 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;
}

您应该提供有关回复本身的更多信息,以便为您的案例找到更好的解决方案。

【讨论】:

  • 谢谢,它成功了,但现在我在 print(jsonResponse[0]['title']); 上得到空值
  • 根据您的print(map) 结果,您的回复格式不正确。我看起来好像响应字段没有用双引号 (") 括起来。例如,context: {categories: [{id: 15, title: Auto-Moto 应该是 "context": {"categories": [{"id": 15, "title": "Auto-Moto" 才能使此解决方案起作用。
猜你喜欢
  • 2022-07-22
  • 1970-01-01
  • 2020-11-25
  • 2021-10-15
  • 2020-07-03
  • 2021-01-30
  • 2019-04-03
  • 2021-09-12
  • 2021-02-25
相关资源
最近更新 更多