如何使用flutter从firestore中检索特定用户详细信息

Mr.*_*boa 5 dart firebase flutter google-cloud-firestore

我是 flutter 和 firebase 的新手,所以请耐心等待。我在我的应用程序上使用电子邮件注册 firestore 和 flutter,在注册时,一些额外的字段被保存到 firestore。我想检索这些字段以显示在用户个人资料上。

保存到用户集合的字段的关键标识符是注册时自动生成的用户 ID。

我在我的小部件构建上下文中有

child: new FutureBuilder<FirebaseUser>(
        future: _firebaseAuth.currentUser(),
        builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            String userID = snapshot.data.uid;
            _userDetails(userID);
            return new Text(firstName);
          }
          else {
            return new Text('Loading...');
          }
        },
      ),
Run Code Online (Sandbox Code Playgroud)

我的获取关联数据方法是:

Future<void> getData(userID) async {
// return await     Firestore.instance.collection('users').document(userID).get();
DocumentSnapshot result = await     Firestore.instance.collection('users').document(userID).get();
return result;
Run Code Online (Sandbox Code Playgroud)

}

检索用户详细信息

void _userDetails(userID) async {
final userDetails = getData(userID);
            setState(() {
                  firstName =  userDetails.toString();
                  new Text(firstName);
});
Run Code Online (Sandbox Code Playgroud)

}

我曾尝试将 .then() 添加到 _userdetails 中的设置状态,但它说 userDetails 是一种无效类型,不能分配给字符串。此处的当前代码块返回“未来”的实例而不是用户详细信息。

die*_*per 3

您的方法已标记为,async因此您必须这样做await才能得到结果:

        Future<void> _userDetails(userID) async {
        final userDetails = await getData(userID);
                    setState(() {
                          firstName =  userDetails.toString();
                          new Text(firstName);
        });
        }
Run Code Online (Sandbox Code Playgroud)