在模型飞镖中调用模型

时间:2020-11-03 08:22:14

标签: flutter dart

我想自己调用模型注释,但不能。 错误:发生异常:类型'_InternalLinkedHashMap '不是类型'Comment'的子类型 请帮助我。

 class Comment {
  Comment({
    this.id,
    this.author,
    this.isAuthor,
    this.isLiked,
    this.content,
    this.stats,
    this.createdAt,
    this.updatedAt,
    this.post,
    this.tagged,
    this.replies,
  });

  String id;
  Author author;
  bool isAuthor;
  bool isLiked;
  String content;
  Stats stats;
  DateTime createdAt;
  String updatedAt;
  String post;
  List<dynamic> tagged;
  List<Comment> replies;

  factory Comment.fromJson(Map<String, dynamic> json) => Comment(
        id: json['_id'] as String,
        author: Author.fromJson(json['author'] as Map<String, dynamic>),
        isAuthor: json['isAuthor'] as bool,
        isLiked: json['isLiked'] as bool,
        content: json['content'] as String,
        stats: Stats.fromJson(json['stats'] as Map<String, dynamic>),
        createdAt: DateTime.parse(json['createdAt'] as String),
        updatedAt: json['updatedAt'] as String,
        post: json['post'] as String,
        tagged: List<dynamic>.from((json['tagged'] as List<dynamic>)
            .map<dynamic>((dynamic x) => x as Map<String, dynamic>)),
        replies: List<Comment>.from((json['replies'] as List<dynamic>)
            .map<dynamic>((dynamic x) => x as Map<String, dynamic>)),
      );
}

1 个答案:

答案 0 :(得分:0)

问题出在您对styles的映射中。您已将回复类型声明为

replies

但是在fromJson方法中,您尝试分配其他类型:

List<Comment> replies;

要更正您必须更改fromJson实现,请执行以下操作:

Map<String, dynamic>