getData() async{
var result = await FirebaseFirestore.instance.collection('users').doc('documentId').get();
print(result['']);
}
@override
void initState() {
getData();
super.initState();
}
In, if the value in result['']
is aa
or 1aa
, the data can be read, but if it is 1.aa
, it cannot be read. How do I solve this?
error code is [VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: field does not exist within the DocumentSnapshotPlatform
#0 DocumentSnapshotPlatform.get._findKeyValueInMap (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:87:7)
#1 DocumentSnapshotPlatform.get._findComponent (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:105:23)
#2 DocumentSnapshotPlatform.get (package:cloud_firestore_platform_interface/src/platform_interface/platform_interface_document_snapshot.dart:121:12)
#3 _JsonDocumentSnapshot.get (package:cloud_firestore/src/document_snapshot.dart:92:48)
#4 _JsonDocumentSnapshot.[] (package:cloud_firestore/src/document_snapshot.dart:96:40)
#5 _jiujitsu_CalState.getData (package:el_sports/JiuJitsu_Page/jiujitsu_cal_page.dart:18:17)
<asynchronous suspension>
I tested the result['']
value with aa
and 1aa
.
How can I bring up the result['1. email']
?
3
Answers
In here use a real document id instead of word
You don’t wanna set the field name using that format which contains some characters such as "." or whitespaces. That’s not a proper naming practice for database fields.
I think the error message is telling that it is looking for the field called
email
which is subfield of the field1
which is not existent.You’re getting that error because you’re using the
.
(dot) character inside the name of the field. Please note that the dot is used as a separator between maps that exist within Cloud Firestore documents. To solve this, stop using the dot within field names. If you need some kind of order, then I recommend you change the following key:to:
And your error will go away.
Besides that, I cannot see a document that has an ID set to
documentId
. As also @MrShakila mentioned in his answer, in order to be able to read the document you have to pass the exact document ID that exists in the database to thedoc()
function: