简体   繁体   English

使用 flutter 从 Firestore 获取数据以获取角色库主页视图

[英]get data from firestore using flutter for role base home view

class Authservice {
handleAuth() {
 return StreamBuilder(
 stream: FirebaseAuth.instance.onAuthStateChanged,
    builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
        var role;
        Firestore.instance
            .collection('users')
            .document(snapshot.data.uid)
            .get()
.then((DocumentSnapshot ds) {
          role = ds.data['role'];
        });
        print('role');
        print(role);
        if (role == 'user') {
          return OwnerHome();
        } else {
          return CustomerHome();
}
      } else {
        return Signin();
      }
    });  
}

role prints a null value i'm trying to show different views depending on the firestore data(role) for a logged in user角色打印 null 值我试图根据登录用户的 Firestore 数据(角色)显示不同的视图

That happens because you are using.then instead of await.发生这种情况是因为您使用的是.then 而不是 await。

When you use.then the code will execute without waiting for the firebase call to complete and that will print your role as null because it doesn't have data.当您使用时,代码将执行而无需等待 firebase 调用完成,并且将您的角色打印为 null,因为它没有数据。 So you'll have to use await to get the user data.所以你必须使用 await 来获取用户数据。

You can either use a future builder or use some other approach to update the UI once you get the user data.获取用户数据后,您可以使用未来的构建器或使用其他方法来更新 UI。

Firebase methods are asynchronous meaning they return a Future that will be resolved in future. Firebase 方法是asynchronous的,这意味着它们返回将来会解决的 Future。 So here in the code Firebase document get method simply return a future that has not been resolved yet, thus printing a null value for role, Since you can not use asyn-await inside of a builder method you can use a FutureBuilder Widget to tackle this issue, the code would look something like this-所以这里在代码 Firebase 文档获取方法中简单地返回一个尚未解决的未来,从而为角色打印 null 值,因为你不能在构建器方法中使用 asyn-await ,你可以使用FutureBuilder小部件来解决这个问题问题,代码看起来像这样 -

class Authservice {
  handleAuth() {
    return StreamBuilder(
      stream: FirebaseAuth.instance.onAuthStateChanged,
      builder: (BuildContext context, snapshot) {
        if (snapshot.hasData) {
          return FutureBuilder<DocumentSnapshot>(
            future: Firestore.instance
                .collection('users')
                .document(snapshot.data.uid)
                .get(),
            builder: (BuildContext context,
                AsyncSnapshot<DocumentSnapshot> snapshot) {
              if (snapshot.hasData) {
                DocumentSnapshot ds = snapshot.data;
                role = ds.data['role'];
                print('role');
                print(role);
                if (role == 'user') {
                  return OwnerHome();
                } else {
                  return CustomerHome();
                }
              } else {
                //Handle whenn user does not exists!
              }
            },
          );
        } else {
          return Signin();
        }
      },
    );
  }
}

As you can see I have added FutureBuilder to the code, this will make Firebase document get method to wait until its future is resolved then only it will proceed with code.如您所见,我已将FutureBuilder添加到代码中,这将使 Firebase 文档获取方法等到其未来得到解决,然后才会继续执行代码。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM