【问题标题】:How to Show name from firebase into flutter screen [duplicate]如何将名称从firebase显示到颤动屏幕中[重复]
【发布时间】:2020-08-05 13:18:29
【问题描述】:
  FirebaseUser loggedinuser;
  FirebaseAuth _auth = FirebaseAuth.instance;

  Future<void> currentUser()async
  {
    try
    {
      FirebaseUser user = await _auth.currentUser();
      if(user != null)
      {
        setState(() {
          loggedinuser  = user;
        });
      }
    }
    catch(e){
      print(e);
    }
  }
  @override
  void initState() {
    super.initState();
    currentUser();
  }



@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Logged in"),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20,vertical: 20),
        child: Text(loggedinuser.providerId)

你好,我是 FLutter 新手,正在做一个小项目来练习 但是每当我尝试显示用户名时,我都会收到一条错误消息,说 Text 小部件不能为空,但我使用 setState 初始化了登录用户 我可以看到电子邮件,但屏幕上看不到名字 感谢您的帮助

【问题讨论】:

  • 因为你是异步加载用户,你需要一个 FutureBuilder。

标签: firebase flutter firebase-authentication


【解决方案1】:

我认为您应该像@nvoigt 所说的那样使用 FutureBuilder。但是您也可以解决这个问题,将这一行 Text(loggedinuser.providerId) 更改为:

Text(loggedinuser != null loggedinuser.providerId : "Loading / Not Logged in")

使用 FutureBuilder 你会得到这样的东西:

class LoadUser extends StatefulWidget {
  @override
  _LoadUserState createState() => _LoadUserState();
}

class _LoadUserState extends State<LoadUser> {
  FirebaseUser loggedinuser;
  FirebaseAuth _auth = FirebaseAuth.instance;

  Future<FirebaseUser> currentUser() async {
    try {
      FirebaseUser user = await _auth.currentUser();
      if (user != null) {
        return user;
      }
      return null;
    } catch (e) {
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FirebaseUser"),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
        child: FutureBuilder<FirebaseUser>(
          future: currentUser(),
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.waiting:
              case ConnectionState.active:
                return Center(child: CircularProgressIndicator(),);
                break;
              case ConnectionState.done:
                loggedinuser = snapshot.data;
                return Text(loggedinuser.providerId);
                break;
              default:
                return Text('Not logged in :(');
                break;
            }
          },
        )
      ),
    );
  }
}

【讨论】:

  • 谢谢你,请问Streambuilder和Futurebuilder一样吗??
【解决方案2】:

您的currentUser() 函数是异步的,实例化loggedinuser 需要时间。所以最初loggedinusernull,这就是你收到错误的原因。要提出解决方案,请将Text 小部件与FutureBuilder 包装在一起。

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Logged in"),
        centerTitle: true,
        backgroundColor: Colors.green,
      ),
      body: Container(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
        child: FutureBuilder(
          future: currentUser(),
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              return Text(loggedInUser.providerId.toString());
            } else if (snapshot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
            }
          },
        ),
      ),
    );
  }

【讨论】:

  • 谢谢你只是想问我可以使用 Streambuilder 代替 Futurebuilder 吗??
  • 如果你的currentUser函数返回Stream,那么你可以使用StreamBuilder。当它返回 Future 时,您可以使用 FutureBuilder。如果我的回答解决了您的问题,请采纳。
猜你喜欢
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 2020-12-27
  • 1970-01-01
相关资源
最近更新 更多