简体   繁体   English

我如何在 function 中使用包含?

[英]how can i use contains inside function?

  void login() async {
    try {
      await auth.signInWithEmailAndPassword(
          email: emailController.text, password: passwordController.text);
      Navigator.push(
        context,
        MaterialPageRoute(builder: (context) => const StudentPage()),
      );
    } catch (e) {
      Fluttertoast.showToast(
          msg: "Tüm alanları doldurunuz.",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 2,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }
}

I want to redirect the users I created with Firebase auth to different pages according to their mail type.我想根据邮件类型将使用 Firebase auth 创建的用户重定向到不同的页面。

For example, if the user is registered with @gmail.com in auth, I want to redirect to one page, if registered with @hotmail.com, to another page.例如,如果用户在身份验证中使用@gmail.com 注册,我想重定向到一个页面,如果使用@hotmail.com 注册到另一个页面。 How can I use contains for this?我怎样才能使用包含这个? Or is there another method for this?或者有另一种方法吗?

   SizedBox(
                  width: 250,
                  height: 50,
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                        primary: const Color.fromARGB(255, 221, 144, 56)),
                    onPressed: () {
                      login();
                    },
                    child: Text('Giriş Yap', 
                        style: GoogleFonts.poppins(
                          fontWeight: FontWeight.w600,
                        )),
                  ),
                ),

I am using the login function here我在这里使用登录 function

There was 2 opportunities to get user's email, you can get it from emailController.text or auth.signInWithEmailAndPassword(..) as it will return Future<UserCredential> , you can took advantage from one of them, here is my example code (adept from yours) with signInWithEmailAndPassword(..)有 2 次机会获得用户的 email,您可以从emailController.textauth.signInWithEmailAndPassword(..)获得它,因为它将返回Future<UserCredential> ,您可以利用其中一个,这是我的示例代码(熟练来自你的)与signInWithEmailAndPassword(..)

void login() async {
    try {
      var userCreds = await auth.signInWithEmailAndPassword(email: emailController.text, password: passwordController.text);
      
      if(userCreds.user != null){
        var user = userCreds.user!;
        if(user.email?.contains("@gmail") ?? false){
          ///Your function for gmail
        }else if(user.email?.contains("@hotmail") ?? false){
          ///Your function for hotmail
        }else {
          ///Add more logic as you need
        }
      }

      // Navigator.push(context, MaterialPageRoute(builder: (context) => const StudentPage()));
    } catch (e) {
      Fluttertoast.showToast(
          msg: "Tüm alanları doldurunuz.",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 2,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }

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

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