Google cloud firestore 即使请求的字段在文档中丢失,也能安全地从Flatter中的cloud firestore获取数据吗?

Google cloud firestore 即使请求的字段在文档中丢失,也能安全地从Flatter中的cloud firestore获取数据吗?,google-cloud-firestore,flutter-web,Google Cloud Firestore,Flutter Web,我的用户集合中有几个文档尚未包含所有可能的字段 从Firestore获取我的用户(使用cloud_Firestore:^2.2.1)时会引发错误 Bad state: field does not exist within the DocumentSnapshotPlatform 我在try-catch块中引发了这个错误 。。。 工厂用户。来自FireStore(文档快照文档){ 试一试{ 打印(doc.get('some-missing-field'); }捕获(e){ 印刷品(e); }

我的用户集合中有几个文档尚未包含所有可能的字段

从Firestore获取我的用户(使用cloud_Firestore:^2.2.1)时会引发错误

Bad state: field does not exist within the DocumentSnapshotPlatform
我在try-catch块中引发了这个错误

。。。
工厂用户。来自FireStore(文档快照文档){
试一试{
打印(doc.get('some-missing-field');
}捕获(e){
印刷品(e);
}
返回用户(
id:doc.id,
电子邮件:doc.get('email')??“,
displayName:doc.get('displayName')??“”,
fieldfoo:doc.get('fieldfoo')??“,
字段栏:doc.get('fieldbar')??[],
);
}
}
...
在哪一点上我没有告诉颤振,它应该用虚拟值填充缺少的字段


有什么最佳实践吗?有人遇到过类似的问题吗?

如果您使用的是空安全设置,您可以得到如下结果:

地图?data=docSnapshot.data();
var值=数据?[“字段名称”]??“”;

解决方案是在每次发生时将DocumentSnapshot映射到

结果函数如下所示

。。。
工厂用户。来自FireStore(文档快照文档){
地图数据=文档数据()!;
返回用户(
id:doc.id,
电子邮件:数据[“电子邮件”]??“,
显示名称:数据['displayName']??“,
语言:数据[“语言”]??“,
附属组织:数据['affiliatedOrganizations']??[],
isAdmin:数据['isAdmin']??错误,
isEditor:数据['isEditor']为假,
);
}
...
此“查询”将生成所有用户的结果

最终流\u用户流=
DatabaseService().usersCollection.snapshots();
当我使用一些用户文档没有的字段进行排序时,这些用户文档将不会显示

最终流\u用户流=
DatabaseService().usersCollection.orderBy('displayName').snapshots();
并非所有用户都有“displayName”,因此他们不会列在我的结果中


有人知道如何绕过它吗?

Object?data()包:cloud\u firestore/cloud\u firestore.dart包含此文档快照的所有数据。无法将“Object”类型的值分配给“Map”类型的变量。尝试更改变量的类型,或将右侧类型强制转换为“Map”。看起来我的快照是这种类型的,但是为什么呢?你能给我们看一下你获取数据的地方的数据库截图吗。