如何在flutter和firebase中使用三种不同的userTypes自动登录? [英] How to do autologin with three diffrent userTypes in flutter and firebase?

查看:62
本文介绍了如何在flutter和firebase中使用三种不同的userTypes自动登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用firebase auth和firestore登录以获取userType的应用程序,此代码显然写在登录页面中,我要添加的是自动登录ASA,该应用程序将运行firebase提供的具有正确userType的应用程序,因此第一个问题是当我在Firestore中用电子邮件搜索以获取userType时如何将电子邮件值传输到main.dart页,第二个问题是当我尝试使用三种不同的userTypes在登录页面中进行自动登录时,它确实登录了但不能自动登录 代码:

I have this app that do login with firebase auth and firestore to get the userType, This code is written obviously in the login page, What I want to add is autologin ASA the app runs which firebase offers with the correct userType So the first proplem how to transfer the email value to the main.dart page as I search in the firestore with the email to get the userType, Second proplem is that When I tried to do auto login in the login page with three different userTypes It does login but not auto login CODE :

@override
  void initState() {
    super.initState();
    FirebaseAuth.instance.currentUser().then(
      (result) {
        if (result != null) {
          if (userType == 'userType1') {
            Navigator.pushReplacementNamed(context, '/userType1page');
          }
          if (userType == 'userType2') {
            Navigator.pushReplacementNamed(context, '/userType2page');
          }
          if (userType == 'userType3') {
            Navigator.pushReplacementNamed(context, '/userType3page');
          }
        }

所以这里得到用户但没有自动登录,我观察到的是当U删除big if and do 1中的其他IF时,它起作用了,所以不知道该怎么办,请帮我,我问了三个问题之前没有得到答案.

So Here It gets the user But no auto login, what I observed that When U remove the other IFs inside the big if and do 1 Navigation It works So don't know what to do, Please Help me I asked three questions before and didn't get an answer.

PS:颤抖的新手:)

PS : NEW TO FLUTTER :)

#FLUTTER_FOREVER

#FLUTTER_FOREVER

从Firestore获取用户数据:

Getting user Data from firestore:

void getUserData() async {
    try {
      firestoreInstance
          .collection('Users')
          .document(usernameController.text)
          .get()
          .then((value) {
        setState(() {
          email = (value.data)['email'];
          password = (value.data)['password'];
          gender = (value.data)['gender'];
          username = (value.data)['username'];
          userType = (value.data)['userType'];
        });
      });
    } catch (e) {
      print(e.toString);
    }
  }

登录:

void login() async {
    final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
    firebaseAuth
        .signInWithEmailAndPassword(
            email: emailController.text, password: passwordController.text)
        .then((result) {
      {
        if (userType == 'Student') {
          Navigator.pushReplacementNamed(context, '/StudentsPage');
        } else if (userType == 'Teacher') {
          Navigator.pushReplacementNamed(context, '/TeacherPage');
        } else if (userType == 'Admin') {
          Navigator.pushReplacementNamed(context, '/AdminPage');
        } else {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Error'),
                content: Text(
                    'Please make sure that you have an internet connection '),
                actions: [
                  FlatButton(
                    child: Text("Ok"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  )
                ],
              );
            },
          );
        }
      }

推荐答案

我找到了必须在initState内部标识var userType的答案,并且通过使用getUserData()函数起作用,但是我遇到了问题不能使用usernameController,因为它是一个var并且我没有定义它,所以任何想法如何使用文档引用usernameController来获取userType,但我无法识别X-('RIP @omardeveloper'

I found the answer I must identify the var userType inside the initState and It worked by using the getUserData() function but I had a problem I can't use usernameController because It's a var and I didn't defined it so any idea how to get the userType with the document reference usernameController Which I can't identify X-( 'R.I.P @omardeveloper'

这篇关于如何在flutter和firebase中使用三种不同的userTypes自动登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆