【问题标题】:_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')_TypeError(类型 'List<dynamic>' 不是类型 'Map<String, dynamic>' 的子类型)
【发布时间】:2020-06-21 16:49:45
【问题描述】:

我正在学习 Dart 和 Flutter。现在我正在品尝 JSON 作为一种持久性方法。我收到很多错误,都与类型和东西有关。这是我遇到的最新错误:_TypeError (type 'List&lt;dynamic&gt;' is not a subtype of type 'Map&lt;String, dynamic&gt;')

这是课程:

import './topic.dart';

class Subject {
  String name;
  int order;
  bool isMajor;
  List<Topic> topics;

  Subject({this.name, this.order, this.isMajor, this.topics});

  factory Subject.fromJSON(Map<String, dynamic> json) {
    if (json != null) {
      return Subject(
          name: json['name'],
          order: json['order'],
          isMajor: json['isMajor'],
          topics: [Topic.fromJSON(json['topics'])]);
    } else {
      return null;
    }
  }
}

主题类是这样的:

import './content.dart';

class Topic {
  String name;
  int order;
  List<Content> contents;

  Topic({this.name, this.order, this.contents});

  factory Topic.fromJSON(Map<String, dynamic> json) {
    if (json != null) {
      return Topic(
          name: json['name'],
          order: json['order'],
          contents: [Content.fromJSON(json['contents'])]);
    } else {
      return null;
    }
  }
}

此处出现错误:[Topic.fromJSON(json['topics'])]

有人可以帮忙吗?谢谢!

【问题讨论】:

  • 请在 json['topics'] 和主题类中添加你得到的内容。

标签: json flutter dart


【解决方案1】:

主题应该是

topics: List<Topic>.from(json["topics"].map((x) => Topic.fromJson(x))),

因为你没有提供Content 类,我假设它有nameorder 属性
可以使用Subject subject = subjectFromJson(jsonString);解析jsonString

完整的相关类

// To parse this JSON data, do
//
//     final subject = subjectFromJson(jsonString);

import 'dart:convert';

Subject subjectFromJson(String str) => Subject.fromJson(json.decode(str));

String subjectToJson(Subject data) => json.encode(data.toJson());

class Subject {
    String name;
    int order;
    bool isMajor;
    List<Topic> topics;

    Subject({
        this.name,
        this.order,
        this.isMajor,
        this.topics,
    });

    factory Subject.fromJson(Map<String, dynamic> json) => Subject(
        name: json["name"],
        order: json["order"],
        isMajor: json["isMajor"],
        topics: List<Topic>.from(json["topics"].map((x) => Topic.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "name": name,
        "order": order,
        "isMajor": isMajor,
        "topics": List<dynamic>.from(topics.map((x) => x.toJson())),
    };
}

class Topic {
    String name;
    int order;
    List<Content> contents;

    Topic({
        this.name,
        this.order,
        this.contents,
    });

    factory Topic.fromJson(Map<String, dynamic> json) => Topic(
        name: json["name"],
        order: json["order"],
        contents: List<Content>.from(json["contents"].map((x) => Content.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "name": name,
        "order": order,
        "contents": List<dynamic>.from(contents.map((x) => x.toJson())),
    };
}

class Content {
    String name;
    int order;

    Content({
        this.name,
        this.order,
    });

    factory Content.fromJson(Map<String, dynamic> json) => Content(
        name: json["name"],
        order: json["order"],
    );

    Map<String, dynamic> toJson() => {
        "name": name,
        "order": order,
    };
}

【讨论】:

  • 您可以在发布到服务器之前使用 subjectToJson 将您的主题对象转换为有效的 json 字符串。
  • 嘿!我应该在我的应用程序的哪个位置调用 toJson() ??
  • 如果只查询数据。不需要,可以参考flutter.dev/docs/development/data-and-backend/json
  • 你能帮我吗? :D 会很高兴的!
  • 我的问题是:如何将 JSON 保存为文件??
猜你喜欢
  • 2021-09-12
  • 2021-02-25
  • 2019-08-18
  • 2020-01-18
  • 2019-09-02
  • 1970-01-01
  • 2021-12-29
  • 2023-01-08
  • 2021-01-02
相关资源
最近更新 更多