[英]How do i get the single field value from querysnapshot in flutter firestore
actually I need to get access to the field called userid which in my firestore collection document but I cant access it.实际上,我需要访问我的 firestore 集合文档中名为 userid 的字段,但我无法访问它。 so, please can anyone help me with this problem?the print statement only prints the data in json..
所以,有人能帮我解决这个问题吗?打印语句只打印 json 中的数据。
void logIn(String email, String password) async {
if (_formKey.currentState!.validate()) {
FirebaseFirestore.instance
.collection("users")
.where("username", isEqualTo: email)
.where("password", isEqualTo: password)
.get()
.then((QuerySnapshot querySnapshot) => {
if (querySnapshot.docs.isNotEmpty)
{
print(querySnapshot.docs.first.data()),
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (_) =>
HomeScreen(userId: querySnapshot.docs.first.id),
this is the out put I get.这是我得到的结果。 but i need only the userid field:
但我只需要 userid 字段:
Calling data()
on a DocumentSnapshot
, gives you all the fields from that document.在
DocumentSnapshot
上调用data()
,会为您提供该文档中的所有字段。 It unfortunately does so as a generic type T?
不幸的是,它作为通用类型
T?
, which is probably where you are struggling. ,这可能是您正在努力的地方。
To get a specific field, you'll want to cast the data()
to a Map
and then look up that field:要获取特定字段,您需要将
data()
转换为Map
,然后查找该字段:
var data = querySnapshot.docs.first.data() as Map;
var value = data["userId"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.