英文:
Error reading Firestore data from flutter
问题
getData() async {
var result = await FirebaseFirestore.instance.collection('users').doc('documentId').get();
print(result['']); // 此处输出了 DocumentSnapshot 中的空字段的值
// 在这里检查 result['1. email'] 并处理它
}
@override
void initState() {
getData();
super.initState();
}
如果 result['1. email']
无法读取并出现错误,可能是因为字段名称中包含空格或特殊字符。你可以尝试使用以下方法来访问这个字段:
var emailValue = result['1. email'];
if (emailValue != null) {
// 处理 emailValue
} else {
// 字段不存在或值为 null
}
这样,你可以检查字段是否存在并处理它的值。如果字段名称中包含特殊字符或空格,确保在访问时使用正确的字段名称。
英文:
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']
?
答案1
得分: 0
使用实际文档标识而不是单词
FirebaseFirestore.instance.collection('users').doc('ox......')//使用您的标识
英文:
In here use a real document id instead of word
FirebaseFirestore.instance.collection('users').doc('ox......')//use your id
答案2
得分: 0
你不想使用包含一些字符,如“.”或空格的格式来设置字段名称。这不是数据库字段的适当命名规范。
我认为错误消息是在告诉它正在查找名为email
的字段,这是字段1
的子字段,但字段1
不存在。
英文:
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 field 1
which is not existent.
答案3
得分: 0
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:
1_email
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 the doc()
function:
var result = await FirebaseFirestore.instance.collection('users').doc('ox...wI').get();
// 👁️👁️
英文:
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:
1. email
to:
1_email
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 the doc()
function:
var result = await FirebaseFirestore.instance.collection('users').doc('ox...wI').get();
// 👆
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论