我用json_serializer创建了一个模型类
@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方法返回该类的列表:
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

发布于 2022-06-18 20:05:16
问题是,您从未在模型类的构造函数中分配属性:
DataBaseModel({
String? word,
String? explain,
List<Phonetics>? phonetics,
List<Meanings>? meanings,
String? uid,
String? path,
Configs? configs,
String? phonetic,
});应:
DataBaseModel({
this.word,
this.explain,
this.phonetics,
this.meanings,
this.uid,
this.path,
this.configs,
this.phonetic,
});https://stackoverflow.com/questions/72672380
复制相似问题