【问题标题】:Flutter Firebase : How to get a user's id by his username?Flutter Firebase:如何通过用户名获取用户 ID?
【发布时间】:2023-01-20 03:23:39
【问题描述】:

我实际上正在开发一个社交应用程序。我想创建一个朋友系统,但我正在为用户搜索而苦苦挣扎。 我希望用户将他朋友的用户名放在 TextField 小部件中,查询 Cloud Firestore (Firebase) 数据库并获取朋友的用户个人资料(如果存在)。

问题是,用户名存储在“Users/USER_ID/profile/username”中,我不知道如何为这个需求查询数据库。

Here is the Cloud Firestore "path" i'm talking about

欢迎提供一点帮助:)

我试过了,但它返回null,可能是因为它没有在正确的地方搜索。

dynamic sUserFromUsername(String username) async {
  try {
    await FirebaseFirestore.instance
      .collection('Users')
      .where('username', isEqualTo: username)
      .get()
      .then((value) => value);
  } catch (exception) {
    print(exception);
  }
}

【问题讨论】:

    标签: android flutter dart google-cloud-firestore


    【解决方案1】:

    usernameprofile 字段中。所以你必须使用profile.username

    FirebaseFirestore.instance
        .collection('Users')
        .where('profile.username', isEqualTo: enteredUsername)
        .get()
        .then((querySnapshot) {
          if (querySnapshot.docs.isNotEmpty) {
            // User profile found, do something with it
            var userProfile = querySnapshot.docs[0].data();
            print(userProfile);
          } else {
            // User profile not found, display an error message
            print('User not found');
          }
        });
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 2021-03-06
      • 1970-01-01
      • 2018-09-24
      • 2019-08-22
      • 2019-03-28
      相关资源
      最近更新 更多