如何在Flutter中使用UID访问数据库? [英] How to use UID to acces database in Flutter?

查看:74
本文介绍了如何在Flutter中使用UID访问数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下函数来检索UID:

i am using the following function to retrive the UID:

  FirebaseAuth auth = FirebaseAuth.instance;

  getUID() async {
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
   return  uid;
  }

之后,我想使用UID访问正确的数据库- doc:

After that, i would like to use the UID to acces the right database-doc:

return new StreamBuilder(
        stream: Firestore.instance.collection('users').document(getUID()).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
          var userDocument = snapshot.data;

但是我得到了错误

Future<dynamic> is not a subtype of type "string"

正确的方法是什么?

完整代码

class Settings extends StatelessWidget{
  FirebaseAuth auth = FirebaseAuth.instance;

  getUID() async {
    final FirebaseUser user = await auth.currentUser();
    final uid = user.uid;
   return  uid;
  }

  static GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Indstillinger"),
      ),
      body: _buildSetting(context),
    );
  }

  Widget _buildSetting(BuildContext context) {
    return new StreamBuilder(
        stream: Firestore.instance.collection('users').document(getUID()).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return new Text("Loading");
          }
          var userDocument = snapshot.data;


推荐答案

您重新传递函数而实际上没有等待返回它。您可以在调用 StreamBuilder 之前将返回值存储在变量中

You're passing the function without actually awaiting for it to return. You could store the returned value in a variable before calling StreamBuilder.

final documentId = await getUID();
return new StreamBuilder(    
                stream: Firestore.instance.collection('users').document(documentId).snapshots(),
                builder: (context, snapshot) {
                  if (!snapshot.hasData) {
                    return new Text("Loading");
                  }
                  var userDocument = snapshot.data;

这篇关于如何在Flutter中使用UID访问数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆