【问题标题】:Getting date from firebase in flutter and store it in a var在颤动中从firebase获取日期并将其存储在var中
【发布时间】:2019-02-24 00:59:54
【问题描述】:

有没有办法可以从 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 的数据

【问题讨论】:

  • 我试过 Future 和 StreamBuild 但他们给了我“instance_of_Future”
  • 请在此处发布您的代码。

标签: firebase firebase-realtime-database flutter


【解决方案1】:

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

.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);
                    });
                  },

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2022-01-13
    • 2019-03-16
    • 2018-07-29
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多