首页 > 解决方案 > 在颤动中从firebase获取日期并将其存储在var中

问题描述

有没有办法可以从 firebase 获取数据并将其存储为字符串,例如在颤振中?

我希望我的应用程序具有基于角色的用户,每个用户都有一个角色和一个页面。firebase 身份验证只有用户名和密码。在我的数据库中,我有用户集合,UID 用作文档的 ID,我想查询文档并获取角色值并将其作为字符串存储在变量中。 http://prntscr.com/kwcylt

 Container(
              height: 50.0,
              child: Material(
                borderRadius: BorderRadius.circular(20.0),
                shadowColor: Colors.greenAccent,
                color: Colors.green,
                elevation: 7.0,
                child: GestureDetector(
                  onTap: () {
                    FirebaseAuth.instance
                        .signInWithEmailAndPassword(
                          email: email,
                          password: password)
                        .then((FirebaseUser user) {
                         var userRole = Firestore.instance.collection('Users').document(user.uid).toString();

                         if(userRole['Role'].toString().compareTo("Admin"))
                            Navigator.of(context).pushReplacementNamed('/AdminTabs');
                          else Navigator.of(context).pushReplacementNamed('/UserTabs');      
                    })
                        .catchError((e) {
                          print(e);
                    });
                  },

我希望 userRole 包含来自 firebase 的数据

标签: firebasefirebase-realtime-databaseflutter

解决方案


像这样更新您的代码,它将起作用:您基本上是在对文档引用而不是文档快照运行检查。

.then((FirebaseUser user) {
    Firestore.instance.collection('Users')
                .document('user.uid').get().then((userRole){
              if(userRole['Role'].toString().contains("Admin")){
                 Navigator.of(context).pushReplacementNamed('/AdminTabs');
              }
    else {Navigator.of(context).pushReplacementNamed('/UserTabs');}
            });

                    })
                        .catchError((e) {
                          print(e);
                    });
                  },

推荐阅读