Flutter:错误状态:DocumentSnapshotPlatform 中不存在该字段

时间:2021-02-05 17:25:44

标签: flutter dart

我正在学习 Flutter 课程,我们目前正在研究 Firestore/Firebase。

在简单社区版的教程中,在线老师使用的Dart/Flutter版本比我用的早一点(我用的是最新的V1.22.6)。输出将在 ListView 中的卡片上放置标题、描述和时间戳

在 Firestore 数据库中,其中一个文档有时间戳字段,而另外两个没有。这个练习是为了展示如何返回一个 null 值或者如果它不存在则什么都不返回。他通过一段代码修复了这个问题,这样它就不会在卡片上返回任何内容:

 Text((snapshot.documents[index].data["timestamp"] == null) ? "" :
snapshot.documents[index].data["timestamp"].toString())

随着 SDK 的更新,我已经修复了语法,但它不会打印 null 或任何内容,它只会出现错误“状态不佳:DocumentSnapshotPlatform 中不存在字段”。我想知道是否有解决方法,以便如果 Firestore 数据库中有一堆文档并且由于某种原因文档中不存在某个字段,那么在卡片上不打印任何内容的最佳方法是什么?

我使用更新语法的代码:

Text((snapshot.docs[index]['timestamp'] == null) ? 'N/A' : 
                snapshot.docs[index]['timestamp'].toString())

错误堆栈:

The following StateError was thrown building CustomCard(dirty):
Bad state: field does not exist within the DocumentSnapshotPlatform

#0      DocumentSnapshotPlatform.get._findKeyValueInMap
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:82
#1      DocumentSnapshotPlatform.get._findComponent
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:100
#2      DocumentSnapshotPlatform.get
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:115
#3      DocumentSnapshot.get
package:cloud_firestore/src/document_snapshot.dart:45
#4      DocumentSnapshot.[]
package:cloud_firestore/src/document_snapshot.dart:52
...

=========================================== ==================================

0 个答案:

没有答案
相关问题