繁体   English   中英

如何在 flutter 和 firebase 中使用不同类型的用户登录?

[英]How to sign in with different types of user in flutter and firebase?

我正在尝试使用不同类型的用户登录,我有 firebase 身份验证器来登录并使用用户和 userType 作为字段创建了一个数据库,所以任何人都知道 function 显示不同类型的用户的不同界面? 我的代码:

class _LoginScreenState extends State<LoginScreen> {
  var email;
  var password;
  var username;
  var gender;
  var userType;
  final usernameController = TextEditingController();
  final emailController = TextEditingController();
  final passwordController = TextEditingController();
  final firestoreInstance = Firestore.instance;
      void login() async {
        final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
        firebaseAuth
            .signInWithEmailAndPassword(
                email: emailController.text, password: passwordController.text)
            .then((result) {
          {
            Navigator.pushReplacementNamed(context, '/homepage');
          }
        }).catchError((err) {
          showDialog(
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text("Error"),
                  content: Text(err.message),
                  actions: [
                    FlatButton(
                      child: Text("Ok"),
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                );
              });
        });
      }
      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);
        }
      }

如果您想在登录后导航到其他页面,则类似于:

final FirebaseAuth firebaseAuth = FirebaseAuth.instance;
firebaseAuth
    .signInWithEmailAndPassword(
        email: emailController.text, password: passwordController.text)
    .then((result) {
  firestoreInstance
      .collection('Users')
      .document(usernameController.text)
      .get()
      .then((value) {
      var userType = (value.data)['userType'];
      if (userType == "firstType") {
        Navigator.pushReplacementNamed(context, '/homepage');
      }
      else if (userType == "secondType") {
        Navigator.pushReplacementNamed(context, '/anotherpage');
      }
    });
  }

暂无
暂无

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

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