首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用于appbar标题的flutter firebase身份验证当前用户电子邮件

用于appbar标题的flutter firebase身份验证当前用户电子邮件
EN

Stack Overflow用户
提问于 2018-09-07 02:29:09
回答 1查看 1.6K关注 0票数 0

用户已使用firebase电子邮件登录登录到应用程序。如何使appbar标题成为当前用户的电子邮件?

代码语言:javascript
复制
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),
          ),
        )
    );
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-07 02:49:21

您需要在拥有新用户后调用setState()

在您的代码中,您创建了Future<String> email,但无法利用最终的user.email计算。

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

代码语言:javascript
复制
FirebaseUser currentUser; // not final

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

代码语言:javascript
复制
FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
   setState(() { // call setState to rebuild the view
     this.currentUser = user;
   });
});

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

  • null (在调用completes)
  • and,well之前,不为空。

因此,您需要确保使用类似于以下逻辑的逻辑处理null案例:

代码语言:javascript
复制
String _email() {
    if (currentUser != null) {
      return currentUser.email;
    } else {
      return "no current user";
    }
}

下面是一个改编自你的代码的例子:

代码语言:javascript
复制
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),
          ),
        ));
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52210184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档