gpt4 book ai didi

flutter - 类型 '_InternalLinkedHashMap'不是类型转换中类型 'Topic'的子类型

转载 作者:行者123 更新时间:2023-12-03 04:24:28 24 4
gpt4 key购买 nike

我对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为您生成所有这些。 Here's文档。希望这可以帮助。

关于flutter - 类型 '_InternalLinkedHashMap<String, dynamic>'不是类型转换中类型 'Topic'的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60501796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com