Flutter Firebase Firestore 如何在 .then() 调用中使用 withConverter()

问题描述 投票:0回答:1

我给 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(), 
);

enter image description here

但是它返回的是

WithConverterDocumentReference
,而不是可以调用
WithConverterDocumentSnapshot
.data()
。我如何通过一次 FB 调用来完成此任务?

flutter firebase dart google-cloud-firestore
1个回答
0
投票

@Dan R 为我指明了正确的方向,我只需将文档 LinkedHashMap 映射到我的类数据即可:

factory LatestStoredJobsEntity.fromDoc(
    Map<String, dynamic> data,
  ) {
    return LatestStoredJobsEntity(
      onSiteTitles: data['onSiteTitles'],
      onSiteUrls: data['onSiteUrls'],
      remoteTitles: data['remoteTitles'],
      remoteUrls: data['remoteUrls'],
    );
  }
© www.soinside.com 2019 - 2024. All rights reserved.