' 不是类型转换中类型 'Topic' 的子类型,flutter,dart"/>

首页 > 解决方案 > 输入'_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;
    }
  }
}

标签: flutterdart

解决方案


如果你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 为您生成所有这些。这是文档。希望这可以帮助。


推荐阅读