Flutter-如何从Firestore返回当前用户 [英] Flutter - How to return current user from Firestore

查看:79
本文介绍了Flutter-如何从Firestore返回当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试从现有的Firestore用户文档中检索经过身份验证的用户详细信息时,出现以下错误:

Trying to retrieve the authenticated user details from the existing Firestore user document, I get the following error:

getter'uid'在null上被调用

The getter 'uid' was called on null

我的代码如下:

    class SideDrawer extends StatelessWidget {
      const SideDrawer({Key key, this.user}) : super(key: key);
      final FirebaseUser user;

      @override
      Widget build(BuildContext context) {
        return ......
              ......
              userPanel(context),
              ......
      }

      Widget userPanel(BuildContext context) {
        return new Padding(
          padding: const EdgeInsets.only(top: 10.0),
          child: new DrawerHeader(
            child: StreamBuilder<DocumentSnapshot>(
              stream: Firestore.instance
                  .collection('users')
                  .document(user.uid)
                  .snapshots(),
              builder:
                  (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Error: ${snapshot.error}');
                } else if (snapshot.hasData) {
                  return userName(snapshot.data);
                }
                return LinearProgressIndicator();
              },
            ),
          ),
        );
      }
    }

Text userName(DocumentSnapshot snapshot) {
  if (snapshot.data == null) {
    return Text('no data set in the user document in firestore');
  } else {
    return snapshot.data['name'];
  }
}

从登录页面发送用户:

  void signIn() async {
    if(_formKey.currentState.validate()){
      _formKey.currentState.save();
      try{
        FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
        Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage(user: user)));
      }catch(e){
        print(e.message);
      }
    }
  }
}

这是我的主页:

class HomePage extends StatelessWidget {
  const HomePage({Key key, this.user}) : super(key: key);
  final FirebaseUser user;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home'),
      ),
      drawer: SideDrawer(),
    );
  }
}

猜到这是一个异步编程问题,我对此进行了搜索,发现很少使用"Future"对象的解决方案,但无法弄清楚如何以正确的方式实现它,请帮忙吗?

Guessing this is an asynchronous programming issue, I did some search on it and found few solutions using "Future" object but could not figure out how to implement it in a correct way, any help please ?

推荐答案

如果您使用的是Future,我猜您在调用SideDrawer时用户为null,因此您必须先添加验证,然后才能使用'user '对象.

If you are using Future I guess when you call SideDrawer the user is null, so you have to add a validation before you can use 'user' object.

 Widget userPanel(BuildContext context) {
            return new Padding(
              padding: const EdgeInsets.only(top: 10.0),
              child:  user != null ? DrawerHeader(
                child: StreamBuilder<DocumentSnapshot>(
                  stream: Firestore.instance
                      .collection('users')
                      .document(user.uid)
                      .snapshots(),
                  builder:
                      (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                    if (snapshot.hasError) {
                      return Text('Error: ${snapshot.error}');
                    } else if (snapshot.hasData) {
                      return userName(snapshot.data);
                    }
                    return LinearProgressIndicator();
                  },
                ),
              ) : Center(child: CircularProgressIndicator(),)
            );

        }

在HomePage的SideDrawer小部件上传递用户对象

Pass the user object on your SideDrawer widget from HomePage

  SideDrawer(user: user),

也请修复此行:

 return snapshot.data['name'];

应该是:

  return Text(snapshot.data['name']);

这篇关于Flutter-如何从Firestore返回当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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