Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting an error "Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast" when I'm trying to login or signup

I'm making a flutter app for my college project, where I'm adding a login and signup page and authenticating it via Firebase, and when I click login the debug console says "Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast" and when I reload the app after this error it successfully logs in.

Everything was working best before the update of the firebase_auth package to 0.12.0 after this update, the methods "signInWithEmailAndPassword()" and "createUserWithEmailAndPassword()" throwing an error "A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser'. Try changing the type of the variable, or casting the right-hand type to 'FirebaseUser'", so I added a cast as FirebaseUser which fixed the error and the app was built successfully but when i clicked on login or create account, debug console said Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast

the main login and create account function code before the update of firebase_auth 0.12.0

Future<String> signIn(String email, String password) async {
FirebaseUser user = await 
FirebaseAuth.instance.signInWithEmailAndPassword(
    email: email, password: password);
return user.uid;
}

Future<String> createUser(String email, String password) async {
FirebaseUser user = await 
FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: email, password: password);
return user.uid;
}

the above code was working fine, after the update (firebase_auth 0.12.0) the same code started throwing this error,

A value of type 'AuthResult' can't be assigned to a variable of type 
'FirebaseUser'.
Try changing the type of the variable, or casting the right-hand type to 
'FirebaseUser'.dart(invalid_assignment)

I fixed the error by casting "FirebaseUser" as shown below

Future<String> signIn(String email, String password) async {
FirebaseUser user = await 
FirebaseAuth.instance.signInWithEmailAndPassword(
    email: email, password: password) as FirebaseUser;
return user.uid;
}

Future<String> createUser(String email, String password) async {
FirebaseUser user = await 
FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: email, password: password) as FirebaseUser;
return user.uid;
}

this new code didn't threw an error in compilation but when I try to login or create new account it throws an error in debug console Error type 'AuthResult' is not a subtype of type 'FirebaseUser' in type cast and the new created account is successfully created on firebase but the app doesn't go on the next page but as soon as i reload it starts with the page that should come after login and creation of account(sign out is working perfectly)

like image 525
Hassan Ansari Avatar asked Aug 01 '19 09:08

Hassan Ansari


3 Answers

This is a breaking change in the plugin and its documented here https://pub.dev/packages/firebase_auth#0120

So you shouldn't do any type of casting you just have to refactor your code to adopt the new changes :

FirebaseUser user = (await FirebaseAuth.instance.
signInWithEmailAndPassword(email: email, password: password))
.user;
like image 70
Raouf Rahiche Avatar answered Sep 29 '22 01:09

Raouf Rahiche


I face the same issue. Just modify to be:

final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;

See https://github.com/flutter/flutter/issues/38757#issuecomment-522307525

like image 34
Ahmed fathi Avatar answered Sep 29 '22 01:09

Ahmed fathi


The classes have been renamed. 'AuthResult' has been renamed to 'UserCredential' and 'FirebaseUser' is renamed to 'User'.

like image 45
pushkar mahajan Avatar answered Sep 29 '22 01:09

pushkar mahajan