简体   繁体   中英

How to retrieve the email ID from firebase in flutter?

My login page

class _MyLoginState extends State<MyLogin> {

  final FirebaseAuth _auth = FirebaseAuth.instance;

  //editing controller
  final TextEditingController emailController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  int _success = 1 ;
  late String _userEmail = "";

  void _signIn() async {
    final User? user = (await _auth.signInWithEmailAndPassword(
        email: emailController.text, password: passwordController.text)
    ).user;

    if (user != null) {
      setState(() {
        _success = 2;
        _userEmail = user.email!;
      });
    }else {
      setState(() {
        _success = 3;
      });
    }
  }

My profile screen for a user

class _UserAccState extends State<UserAcc> {


  final _auth = FirebaseAuth.instance;
  late String userEmail;
  void getCurrentUserEmail() async {
    final user =
    _auth.currentUser().then((value) => userEmail = value.email);
  }

  late final TextEditingController emailController;

The code in UserAcc that i have posted, will it retrieve the email id from firebase? If not, tell me how to do it. I'm a beginner at flutter. I'm very much confused.

This is very close to being correct, but to get the Email, you must use a function which actually returns a value (String in this case).

Right now you are setting the 'user' variable to the email, but it has not been returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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