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

后端 未结 6 1362
执笔经年
执笔经年 2021-01-11 10:00

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

相关标签:
6条回答
  • 2021-01-11 10:18

    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.

    0 讨论(0)
  • 2021-01-11 10:20

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

    0 讨论(0)
  • 2021-01-11 10:24
    • use User instead of FirebaseUser on firebase_auth 0.18.0+1

      final User user = (await FirebaseAuth.instance.signInWithEmailAndPassword(
                      email: 'abc@gmail.com',
                      password: 'abc',
                     )).user;
      
    0 讨论(0)
  • 2021-01-11 10:26

    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}');
    
    0 讨论(0)
  • 2021-01-11 10:28

    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

    0 讨论(0)
  • 2021-01-11 10:30

    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;
    
    0 讨论(0)
提交回复
热议问题