-->

I'm getting an error “Error type 'AuthResu

2020-08-13 05:09发布

问题:

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)

回答1:

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;


回答2:

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



回答3:

I had same error and changed code to:

FirebaseUser user = (await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password)).user;

The app works as expected on a real device, without an error in the console, but Visual Studio Code shows red underline at the end below user and outputs an error:

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

[Solution] Restart VS Code. VS Code uses the old library until restart.



回答4:

It has been changed what you should do is AuthResult result = await _firebaseAuth.signInWithEmailAndPassword( email: email, password: password); FirebaseUser user = result.user;



回答5:

signInWithEmailAndPassword returns AuthResult and AuthResult class contains instance variable user of type FirebaseUser. So You have to change your code as following way.

FirebaseUser userDetails = (await FirebaseAuth.instance
          .signInWithEmailAndPassword(Credentialsxxx)).user;
print('sign in : ${userDetails.uid}');