flutter firestore 数据读取

huangapple go评论61阅读模式
英文:

flutter firestore data read

问题

getData() async {
    var a = await FirebaseFirestore.instance.collection('users').where('1_email', isEqualTo: currentUser.currentUser!.email).get();
    print(a);
  }
@override
  void initState() {
   getData();
    super.initState();
  }

Console:

> flutter: Instance of 'QuerySnapshot<Map<String, dynamic>>'

这段代码是用来从Firebase中拉取数据的,如果用户登录的邮箱与Firestore数据库中的邮箱匹配,请拉取数据。

然而,如果您尝试使用 print(a['1_email']),将会出现以下错误(如附图所示):

> 运算符 '[]' 未定义于类型 'QuerySnapshot<Map<String, dynamic>>'。 (Documentation) 请定义运算符 '[]'。

您想要如何获取下面的数据?

flutter firestore 数据读取


<details>
<summary>英文:</summary>

getData() async {
var a = await FirebaseFirestore.instance.collection('users').where('1_email', isEqualTo:currentUser.currentUser!.email).snapshots();
print(a);
}
@override
void initState() {
getData();
super.initState();
}

Console : 

&gt; flutter: Instance of &#39;_MapStream&lt;QuerySnapshotPlatform, QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&gt;&#39;


getData() async {
var a = await FirebaseFirestore.instance.collection('users').where('1_email', isEqualTo:currentUser.currentUser!.email).get();
print(a);
}
@override
void initState() {
getData();
super.initState();
}

Console : 

&gt; flutter: Instance of &#39;_JsonQuerySnapshot&#39;


The code above is, if the user&#39;s email logged in to the Firebase matches the email contained in the Firestore database, please pull the data

However, if you treat it as `print(a[&#39;1_email])`
the following error appears (pictured attached)

&gt; The operator &#39;[]&#39; isn&#39;t defined for the type &#39;QuerySnapshot&lt;Map&lt;String, dynamic&gt;&gt;&#39;. (Documentation)  Try defining the operator &#39;[]&#39;.

![Error](https://i.stack.imgur.com/eoklx.png)

How can I get the data below?

![Firestore](https://i.stack.imgur.com/fMp6Q.png)




</details>


# 答案1
**得分**: 0

你正在执行一个查询,该查询可能会有多个结果。您的代码没有处理可能存在多个结果的情况。

为了正确处理一次性读取操作,可以使用以下代码更改:
```dart
getData() async {
  var querySnapshot = await FirebaseFirestore.instance.collection('users').where('1_email', isEqualTo: currentUser.currentUser!.email).get();
  for (var docSnapshot in querySnapshot.docs) {
    print(docSnapshot.get("1_email"));
  }
}

这个代码更改基本上可以从Firebase文档中关于执行查询的部分进行复制/粘贴。

英文:

You're executing a query, which can have multiple results. Your code doesn't handle the fact that there may be multiple results.

To handle this correctly with a one-time read operation, would be:

getData() async {
  var querySnapshot = await FirebaseFirestore.instance.collection(&#39;users&#39;).where(&#39;1_email&#39;, isEqualTo:currentUser.currentUser!.email).get();
  for (var docSnapshot in querySnapshot.docs) {
    print(docSnapshot.get(&quot;1_email&quot;));
  }
}

The code change is pretty much copy/paste from the Firebase documentation section on executing a query.

huangapple
  • 本文由 发表于 2023年6月15日 08:39:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定