flutter - 输入'_InternalLinkedHashMap' 不是类型转换中类型 'Topic' 的子类型
问题描述
我对 Flutter 很陌生,想渲染一个 ListView。但我得到一个错误。
我有 3 个模型类:
主题、主题和内容。
这是模型文件topic.dart
:
import './content.dart';
class Topic {
String name;
List<Content> contents;
Topic(this.name, this.contents);
factory Topic.fromJSON(Map<String, dynamic> json) {
if (json != null) {
return Topic(json['name'], json['contents'].cast<Content>());
} else {
return null;
}
}
}
这是topics_page.dart
文件:
import 'package:flutter/material.dart';
import './../models/subject.dart';
class TopicsPage extends StatefulWidget {
final Subject _subject;
TopicsPage(this._subject);
@override
State<StatefulWidget> createState() {
return _TopicsPageState();
}
}
class _TopicsPageState extends State<TopicsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget._subject.name),
),
body: Container(
child: ListView.separated(
separatorBuilder: (BuildContext context, int index) => Divider(
color: Colors.grey[200],
thickness: 1.5,
),
itemCount: widget._subject.topics.length,
itemBuilder: (BuildContext context, int index) {
return InkWell(
onTap: () {
Navigator.of(context).pushNamed('/contents',
arguments: widget._subject.topics[index]);
},
child: ListTile(
title: Text(
widget._subject.topics[index].name,
style: TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.w500,
letterSpacing: 0.3),
),
),
);
},
),
));
}
}
问题出现在widget._subject.topics[index].name
。
错误信息是:type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Topic' in type cast
现在我需要一些帮助来找出可能的解决方案!
有什么想法吗?
谢谢!
更新
content.dart
:
class Content {
String title;
String body;
Content(this.title, this.body);
factory Content.fromJSON(Map<String, dynamic> json) {
if (json != null) {
return Content(json['title'], json['body']);
} else {
return null;
}
}
}
subject.dart
import './topic.dart';
class Subject {
String name;
List<Topic> topics;
Subject(this.name, this.topics);
factory Subject.fromJSON(Map<String, dynamic> json) {
if (json != null) {
return Subject(json['name'], json['topics'].cast<Topic>());
} else {
return null;
}
}
}
这是我的其他模型文件。
请告诉我我有什么问题!谢谢!
更新 2
现在我收到错误消息:The argument type 'Content' can't be assigned to the parameter type 'List<Topic>'
...
这是您建议的新代码:
import './content.dart';
class Topic {
String id;
String name;
int order;
bool isImportant;
List<Topic> contents;
Topic({this.id, this.name, this.order, this.isImportant, this.contents});
factory Topic.fromJSON(Map<String, dynamic> json) {
if (json != null) {
return Topic(
name: json['name'],
order: json['order'],
isImportant: json['isImportant'],
contents: Content.fromJSON(json['contents'] as Map<String, dynamic>));
} else {
return null;
}
}
}
解决方案
如果你Content
是一个类,你的主题内容应该映射json['contents']
到你的Content
工厂
在您的内容中
factory Content.fromJSON(Map<String, dynamic> json) {
if (json != null) {
return Content(json['content1']);
} else {
return null;
}
}
在您的主题中:
factory Topic.fromJSON(Map<String, dynamic> json) {
if (json != null) {
return Topic(json['name'], Content.fromJson(json['content'] as Map<String, dynamic>);
} else {
return null;
}
}
最后,在定义类之后,您实际上可以使用 JsonSerializer 为您生成所有这些。这是文档。希望这可以帮助。
推荐阅读
- c - 缓存命中和缓存未命中测量 ARMV8
- android - 如何测试从其他类方法内部获取值的方法
- javascript - 删除在 for 循环中找到的匹配项
- javascript - 遍历数组会产生不同的结果
- c++ - 将polydat拆分为VTK中的所有连接组件
- python - Python:TypeError:'numpy.ndarray'和'int'的实例之间不支持'>'
- python - 将 apache-airflow 库添加到 PyCharm
- oracle - Oracle查询优化匹配/搜索ID与多个表的第一个匹配
- javascript - 在角度上下文之外使用带有香草 javascript 的 html
- abap - 如何将新行添加到嵌套内部表中?