首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >颤振:列表上的映射方法无效

颤振:列表上的映射方法无效
EN

Stack Overflow用户
提问于 2022-06-18 20:01:38
回答 1查看 62关注 0票数 0

我用json_serializer创建了一个模型类

代码语言:javascript
复制
@JsonSerializable()
class DataBaseModel {
  String? word;
  String? explain;
  List<Phonetics>? phonetics;
  List<Meanings>? meanings;
  String? uid;
  String? path;
  Configs? configs;
  String? phonetic;
  DataBaseModel({
    String? word,
    String? explain,
    List<Phonetics>? phonetics,
    List<Meanings>? meanings,
    String? uid,
    String? path,
    Configs? configs,
    String? phonetic,
  });

  factory DataBaseModel.fromJson(Map<String, dynamic> json) =>
      _$DataBaseModelFromJson(json);

  Map<String, dynamic> toJson() => _$DataBaseModelToJson(this);
}

现在,我想作为这个类创建一个实例,因此在我的一个列表中,我使用Map方法返回该类的列表:

代码语言:javascript
复制
final vocabs = dbclient.bularyBox.values
    .toList()
    .map((e) => DataBaseModel(
          uid: "e.uid ?? " "",
          word: "e.word",
          path: "e.path",
          explain: "e.explain",
          phonetic: "e.phonetic",
     
        ))
    .toList();

final newjson = vocabs.map((e) => e.toJson()).toList();

但是vocabs属性是null

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-18 20:05:16

问题是,您从未在模型类的构造函数中分配属性:

代码语言:javascript
复制
DataBaseModel({
  String? word,
  String? explain,
  List<Phonetics>? phonetics,
  List<Meanings>? meanings,
  String? uid,
  String? path,
  Configs? configs,
  String? phonetic,
});

应:

代码语言:javascript
复制
DataBaseModel({
  this.word,
  this.explain,
  this.phonetics,
  this.meanings,
  this.uid,
  this.path,
  this.configs,
  this.phonetic,
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72672380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档