【问题标题】:Flutter: How to fetch a particular record from firebaseFlutter:如何从 firebase 获取特定记录
【发布时间】:2021-08-30 15:57:04
【问题描述】:

这是我的数据库截图。

我想获取特定用户名的记录。 (例如:where user=nam@gmail.com)任何人都可以向我建议如何在颤振中获取这个吗?

这将是一个很大的帮助,谢谢。

【问题讨论】:

标签: database firebase flutter google-cloud-firestore


【解决方案1】:

要获取数据,您可以创建如下函数:

Future getData(String username) async {
    List dataList = [];
    try {
      await FirebaseFirestore.instance.collection('userdata').where('user', isEqualTo: username).get().then((QuerySnapshot querySnapshot) => {
            querySnapshot.docs.forEach((doc) {
              itemList.add(doc.data());
            }),
          });
      return itemList;
    } catch (e) {
      print(e.toString());
      return null;
    }
  }

当你调用这个函数时,你必须传递username,它会返回一个数据项列表。

然后可以使用此列表在 UI 中将数据显示为:

 child: Text(
            title: Text(subjectList[index]['user']),
           ),

【讨论】:

【解决方案2】:

你可以试试这个approach 它对我有帮助,但是如果你已经迁移到空安全,请确保你相应地更改代码

【讨论】:

    【解决方案3】:

    您可以尝试以下查询来获取结果。

    FirebaseFirestore.instance
      .collection('your-collection-name')
      .where('user', arrayContains: 'nam@gmail.com')
      .get();
    
    

    【讨论】:

    • Future user = FirebaseFirestore.instance .collection('userdata') .where('user', arrayContains: email) .get();这有帮助,但你能告诉我如何使用用户打印我的数据吗?
    • 如果您想从 firebase 集合中获取/打印所有 user 键值。您可以简单地使用不带条件语句arrayContains: email 的相同查询。 Future<QuerySnapshot> user = FirebaseFirestore.instance .collection('userdata') .where('user').get();
    • 如何获取特定的键值对?假设,我想获取用户的权重,该怎么做?
    • 嗨@MahishaPatel,在Firebase 中,我们将值存储在地图类型中。所以,如果你想得到一个用户的权重,你应该将键(权重)传递给 where 方法。喜欢FirebaseFirestore.instance .collection('userdata') .where('weight').get();
    • 当我打印用户时,我得到这个:Instance of 'Future<QuerySnapshot<Map<String, dynamic>>>'
    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 2021-06-23
    • 2020-11-05
    • 2021-02-01
    • 2020-09-20
    • 2021-07-04
    • 1970-01-01
    • 2020-04-20
    相关资源
    最近更新 更多