使用Flutter和Firebase进行基于角色的身份验证和导航

时间:2019-05-29 01:45:55

标签: firebase authentication flutter dart google-cloud-firestore

我正在尝试创建一个具有基于角色的身份验证服务的Flutter应用程序。当用户点击登录按钮时,我想从Firestore返回bool值并导航到特定屏幕。但是我尝试了一些代码,但是没有用。

它显示的是空布尔值。

Future<void> Login() async {
    final formState = _formKey.currentState;
    if (formState.validate()) {
      formState.save();
      try {
        FirebaseUser user = await FirebaseAuth.instance
            .signInWithEmailAndPassword(email: _email, password: _password);
        if (user.isEmailVerified) {
        Firestore.instance
              .collection('allusers')
              .where('email', isEqualTo: _email)
              .snapshots()
              .listen((data) =>
                  data.documents.forEach((doc) {
                    if(doc['isAdmin']){
                      Navigator.pushReplacement(
                          context, MaterialPageRoute(builder: (context) => Dashboard()));
                    }else{
                      Navigator.pushReplacement(
                          context, MaterialPageRoute(builder: (context) => AddProduct()));
                    }

                  }));

        } else {
          Scaffold.of(context).showSnackBar(new SnackBar(
            backgroundColor: Color.fromRGBO(255, 87, 51, 1),
            content: new Text('Please verify your email address!'),
          ));
          await FirebaseAuth.instance.signOut();
        }
      } catch (e) {
        Scaffold.of(context).showSnackBar(new SnackBar(
          backgroundColor: Color.fromRGBO(255, 87, 51, 1),
          content: new Text(e.message),
        ));
      }
    }
  }

0 个答案:

没有答案