0

Hello everyone can anyone please help me,

at first i wrote

 Future<void> signIn() async {
    //we validate fields here
    final formState = _formKey.currentState;
    if (formState!.validate()) {
      // login to firebase
      formState.save();
      FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
    }
  }

then i read that "Firebaseuser" became "User", so after changing it i got errors in FirebaseAuth.instance

A value of type 'UserCredential' can't be assigned to a variable of type 'User'.
Try changing the type of the variable, or casting the right-hand type to 'User'

so then, i changed FireBaseAuth to UserCredential, so i got another error

The getter 'instance' isn't defined for the type 'UserCredential'.
Try importing the library that defines 'instance', correcting the name to the name of an existing getter, or defining a getter or field named 'instance'

What should i do? and how do i re-write my code so it works?

Nadia Nadou
  • 137
  • 2
  • 7
  • Does this answer your question? [Error in FirebaseUser for authentication using flutter](https://stackoverflow.com/questions/66760058/error-in-firebaseuser-for-authentication-using-flutter) – Simon Sot Jun 11 '21 at 20:37
  • @SimonSot thank you, but no i tried it and i get erroros in firebaseAuth.instance – Nadia Nadou Jun 11 '21 at 20:58
  • The thing is that you are most likely get that code from outdated source. Some time ago firebaseAuth used class `FirebaseUser`, that is now deprecated. And now getter for user is `FirebaseAuth.instance.currentUser`, that will get you a class `User`. Please check this link it has all data to learn about work with this api https://firebase.flutter.dev/docs/auth/usage – Simon Sot Jun 11 '21 at 21:11
  • oh okay thank you so much, and yes you are right i'm learning from a video of 2018 – Nadia Nadou Jun 11 '21 at 21:16
  • 1
    most likely you will be interested in usage part - email/password register and signin, there is a code snippet that you need. – Simon Sot Jun 11 '21 at 21:19

1 Answers1

1
try {
  UserCredential userCredential = await 
  FirebaseAuth.instance.signInWithEmailAndPassword(
    email: _email,
    password: _password
  );
 User user = userCredential.user; 
 print(user);
} on FirebaseAuthException catch (e) {
  if (e.code == 'user-not-found') {
    print('No user found for that email.');
  } else if (e.code == 'wrong-password') {
    print('Wrong password provided for that user.');
  }
}

FirebaseUser has been replaced with User.

nitishk72
  • 1,616
  • 3
  • 12
  • 21
  • thank you i tried it but then i got an error telling me to change 'User user = userCredential.user ' to 'User? user = userCredential.user ', and i guess now it works – Nadia Nadou Jun 11 '21 at 21:01