我正在使用 json_serializable 和 firestore。json_serializable 创建一个以 Map<String, dynamic> 作为参数的工厂方法,但在 nullsafety 更改后,firestore 开始返回 Map<String, dynamic>?作为文档数据,现在我无法调用 json_serializable 工厂来映射我的 Firestore 类型,因为显示此错误消息:The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>'
有人可以帮我弄这个吗?我不能再将 json_serializable 与 firestore 一起使用吗?
我写了这个例子:
class Entity {
const Entity({required this.id});
final String id;
factory Entity.fromJson(Map<String, dynamic> json) => _$EntityFromJson(json);
Map<String, dynamic> toJson() => _$EntityToJson(this);
}
class Repository {
Stream<List<Entity>> list() {
return FirebaseFirestore.instance.collection('entity').snapshots().map((event) {
return event.docs.map((e) {
return Entity.fromJson(
e.data() // The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>'
);
}).toList();
});
}
}