我给 firestore 打电话:
fireDb
.collection(FIRE_DB_ROOT_JOBS)
.doc(fireAuthUseCases.getEmail())
.collection(FIRE_DB_LATEST_JOBS)
.get()
.then((QuerySnapshot snapshot) {
if (snapshot.docs.isNotEmpty) {
latestJobsDocId = snapshot.docs[0].id;
}
}),
然后我使用latestJobsDocId再次调用firestore,然后像这样调用withConverter():
fireDb
.collection(FIRE_DB_ROOT_JOBS)
.doc(fireAuthUseCases.getEmail())
.collection(FIRE_DB_LATEST_JOBS)
.doc(latestJobsDocId)
.withConverter<LatestStoredJobsEntity>(
...
...
我试图弄清楚如何消除第二次调用,只需在 .then((QuerySnapshot snapshot) 内使用转换器进行调用,但我无法让 withConverter 正常工作。我尝试过:
snapshot.docs[0].reference.withConverter<LatestStoredJobsEntity>(
fromFirestore: LatestStoredJobsEntity.fromFirestore,
toFirestore: (LatestStoredJobsEntity jobsEntity, _) => jobsEntity.toFirestore(),
);
但是它返回的是
WithConverterDocumentReference
,而不是可以调用WithConverterDocumentSnapshot
的.data()
。我如何通过一次 FB 调用来完成此任务?
@Dan R 为我指明了正确的方向,我只需将文档 LinkedHashMap
factory LatestStoredJobsEntity.fromDoc(
Map<String, dynamic> data,
) {
return LatestStoredJobsEntity(
onSiteTitles: data['onSiteTitles'],
onSiteUrls: data['onSiteUrls'],
remoteTitles: data['remoteTitles'],
remoteUrls: data['remoteUrls'],
);
}