Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

dart - Flutter: Bad state: field does not exist within the DocumentSnapshotPlatform

I'm doing a Flutter course and we are currently looking at Firestore/Firebase.

In the tutorial for a simple community board, the online instructor uses a version of Dart/Flutter which is a little earlier than the one I am using (I'm using the latest V1.22.6). The out put will put a title, description and timestamp on cards in a ListView

In the Firestore DB, one of the documents has a timestamp field and the other two do not. This exercise was to show how to return a null value or nothing if it doesn't exist. He fixes this by a piece of code so that it just returns nothing on the card:

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

With the updates to the SDK, I have fixed the syntax, but it won't print null or anything, it just falls flat with the error 'Bad state: field does not exist within the DocumentSnapshotPlatform'. I'm wondering if there's a workaround so that if there's a bunch of documents in a Firestore DB and for whatever reason a field does not exist in the document, what is the best way just to print nothing on the card?

My code with the updated syntax:

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

The error stack:

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
...

════════════════════════════════════════════════════════════════════════════════

question from:https://stackoverflow.com/questions/66067832/flutter-bad-state-field-does-not-exist-within-the-documentsnapshotplatform

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...