Flutter 将参数传递给withConverter方法cloud\u firestore 2.1.0的最佳方法
您好,我正在尝试迁移到Flutter 将参数传递给withConverter方法cloud\u firestore 2.1.0的最佳方法,flutter,dart,google-cloud-firestore,Flutter,Dart,Google Cloud Firestore,您好,我正在尝试迁移到cloud\u firestore 2.10,文档中说要这样实现: final modelsRef = FirebaseFirestore .instance .collection('models') .withConverter<Model>( fromFirestore: (snapshot, _) => Model.fromJson(snapshot.data()!), toFirestor
cloud\u firestore 2.10
,文档中说要这样实现:
final modelsRef = FirebaseFirestore
.instance
.collection('models')
.withConverter<Model>(
fromFirestore: (snapshot, _) => Model.fromJson(snapshot.data()!),
toFirestore: (model, _) => model.toJson(),
);
Future<void> main() async {
// Writes now take a Model as parameter instead of a Map
await modelsRef.add(Model());
final Model model = await modelsRef.doc('123').get().then((s) => s.data());
}
final modelsRef=FirebaseFirestore
.例如
.collection('模型')
.带转换器(
fromFirestore:(快照,)=>Model.fromJson(snapshot.data()!),
toFirestore:(model,)=>model.toJson(),
);
Future main()异步{
//写入现在将模型作为参数,而不是映射
等待modelsRef.add(Model());
最终模型=等待模型ref.doc('123').get()。然后((s)=>s.data());
}
但是在我的例子中,我将参数传递给我的模型,而不仅仅是Model.fromJson(snapshot.data()!)
我使用的示例是GeneralProductModel.fromMap(snap.data()!,parameter1,parameter2),
最好的办法是什么?要使用方法
而不是final modelRef=
但是如果我将notnull
参数传递给该方法,并且仅在读取时需要它,而在写入时不需要它
提前谢谢