【问题标题】:flutter firebase auth current user email for appbar titleflutter firebase auth当前用户电子邮件的appbar标题
【发布时间】:2018-09-06 18:29:09
【问题描述】:

用户已使用 firebase 电子邮件登录方式登录应用。 如何让 appbar 标题成为当前用户的邮箱?

class _HomePageState extends State<HomePage> {

  final  Future<String> userEmail = FirebaseAuth.instance.currentUser().then((FirebaseUser user) => user.email);
  var e = "";

  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: new AppBar(
        backgroundColor: Colors.black,
          title: Text('userEmail'),
          actions: <Widget>[
            new FlatButton(
                onPressed: _signOut,
                child: new Text('logout', style: new TextStyle(fontSize: 17.0, color: Colors.white))
            ),

          ],
        ),
        body: new Center(
          child: new Text(
            e,
            style: new TextStyle(fontSize: 32.0),
          ),
        )
    );
  }
}

【问题讨论】:

    标签: firebase firebase-authentication flutter


    【解决方案1】:

    拥有新用户后,您需要致电setState()

    在您的代码中,您创建了 Future&lt;String&gt; email,但无法利用最终的 user.email 评估。

    我建议创建一个非最终变量来保存您的FirebaseUser,然后在StateinitState() 方法中触发对此的请求。

    FirebaseUser currentUser; // not final
    

    目标是最终能够收到 FirebaseUser 后调用setState()(因为这是异步的)。

    FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
       setState(() { // call setState to rebuild the view
         this.currentUser = user;
       });
    });
    

    需要注意的重要一点是,您必须为currentUser 的所有可能状态构建您的 UI,它们是:

    • null(通话完成前)
    • 而且,嗯,不为空。

    因此,您需要确保处理 null 的情况,其逻辑类似于:

    String _email() {
        if (currentUser != null) {
          return currentUser.email;
        } else {
          return "no current user";
        }
    }
    

    以下是根据您的代码改编的示例:

    class _HomePageState extends State<HomePage> {
      FirebaseUser currentUser;
    
      @override
      void initState() {
        super.initState();
        _loadCurrentUser();
      }
    
      void _loadCurrentUser() {
        FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
          setState(() { // call setState to rebuild the view
            this.currentUser = user;
          });
        });
      }
    
      String _email() {
        if (currentUser != null) {
          return currentUser.email;
        } else {
          return "no current user";
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
            appBar: new AppBar(
              backgroundColor: Colors.black,
              title: Text('userEmail'),
              actions: <Widget>[
                new FlatButton(
                    onPressed: _signOut,
                    child: new Text('logout',
                        style: new TextStyle(fontSize: 17.0, color: Colors.white))),
              ],
            ),
            body: new Center(
              child: new Text(
                _email(),
                style: new TextStyle(fontSize: 32.0),
              ),
            ));
      }
    }
    

    【讨论】:

    • 非常感谢,它运行良好。我想再问你一个问题。我什么时候应该使用异步?我以为我应该在我的情况下使用异步函数,但事实并非如此。
    • 如果你想(我愿意)你可以用 async 关键字标记_loadCurrentUser()async{ 。然后,您可以删除 .then() 调用,而在前面使用 await
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 2019-08-12
    • 2019-09-14
    • 1970-01-01
    • 2021-10-21
    • 2020-03-21
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多