为什么文档不存在。但所有时间 !snapshot.hasData 都是假的
Why document doesn't exist. But all time !snapshot.hasData is false
我制作了注册系统和管理用户信息。
注册时需要填写用户名,保存在firestore中
用户数据是这样存储的;
并尝试使用代码获得 userName
;
CollectionReference users = FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(FirebaseAuth.instance.currentUser!.uid).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
print("${!snapshot.hasData} ${!snapshot.data!.exists}"); // always "false true"
if (!snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Text("User Name: ${data['userName']}");
}
return const CircularProgressIndicator();
},
);
但是总是调用return Text("Document does not exist");
,userName
不显示。
为什么 userName
没有返回?我搜索了一个拼写错误,但找不到。
这个问题今天花了很多时间。谢谢。
想在snapshot.hasData
之前使用!
if (!snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
你说的snapshot.data
就是用来监控异步操作状态的AsyncSnapshot
。当异步操作完成时,它的 hasData
变为真:所以当它完成从数据库读取 DocumentSnapshot
时。
要检测DocumentSnapshot
是否真的存在,需要检查它的exists
属性.
常见状态的快速总结:
Object.property
Value
Explanation
AsyncSnapshot.hasData
false
the asynchronous operation has either not yet completed, or did not result in a DocumentSnapshot
(for example: if there was a network error). Calling snapshot.data
will result in an error message.
AsyncSnapshot.hasData
true
the asynchronous operation has completed and you can safely call snapshot.data
on it to get the DocumentSnapshot
.
DocumentSnapshot.exists
false
the document you requested does not exist, and calling document.data
on the snapshot will result in an error.
DocumentSnapshot.exists
true
the document you requested exists, and you can access its data.
因此,一旦 AsyncSnapshot
完成加载 DocumentSnapshot
,您的 !snapshot.hasData
将为假。要确定文档是否实际存在,请使用 snapshot.hasData && snapshot.data!.exists
.
我还建议您在这里查看我对 Flutter 代码中各种快照类型的解释:
我制作了注册系统和管理用户信息。
注册时需要填写用户名,保存在firestore中
用户数据是这样存储的;
并尝试使用代码获得 userName
;
CollectionReference users = FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(FirebaseAuth.instance.currentUser!.uid).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
print("${!snapshot.hasData} ${!snapshot.data!.exists}"); // always "false true"
if (!snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return Text("User Name: ${data['userName']}");
}
return const CircularProgressIndicator();
},
);
但是总是调用return Text("Document does not exist");
,userName
不显示。
为什么 userName
没有返回?我搜索了一个拼写错误,但找不到。
这个问题今天花了很多时间。谢谢。
想在snapshot.hasData
!
if (!snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
你说的snapshot.data
就是用来监控异步操作状态的AsyncSnapshot
。当异步操作完成时,它的 hasData
变为真:所以当它完成从数据库读取 DocumentSnapshot
时。
要检测DocumentSnapshot
是否真的存在,需要检查它的exists
属性.
常见状态的快速总结:
Object.property | Value | Explanation |
---|---|---|
AsyncSnapshot.hasData |
false |
the asynchronous operation has either not yet completed, or did not result in a DocumentSnapshot (for example: if there was a network error). Calling snapshot.data will result in an error message. |
AsyncSnapshot.hasData |
true |
the asynchronous operation has completed and you can safely call snapshot.data on it to get the DocumentSnapshot . |
DocumentSnapshot.exists |
false |
the document you requested does not exist, and calling document.data on the snapshot will result in an error. |
DocumentSnapshot.exists |
true |
the document you requested exists, and you can access its data. |
因此,一旦 AsyncSnapshot
完成加载 DocumentSnapshot
,您的 !snapshot.hasData
将为假。要确定文档是否实际存在,请使用 snapshot.hasData && snapshot.data!.exists
.
我还建议您在这里查看我对 Flutter 代码中各种快照类型的解释: