【发布时间】:2020-06-21 16:49:45
【问题描述】:
我正在学习 Dart 和 Flutter。现在我正在品尝 JSON 作为一种持久性方法。我收到很多错误,都与类型和东西有关。这是我遇到的最新错误:_TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>')。
这是课程:
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'] 和主题类中添加你得到的内容。