Home > Enterprise >  Async/Await Keeps Getting Invalid Assignment
Async/Await Keeps Getting Invalid Assignment

Time:08-11

So I have a Flutter project that was given to me as an assignment despite the fact that I have no experience whatsoever in FLutter or mobileDev. So for this project, I basically follow tutorials from many sources about making clone apps. But when I follow their code, I always get an error every time the code involves using async and await. Here's an example:

class authFirebase {
  final FirebaseAuth auth = FirebaseAuth.instance;
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;

  getCurrentUser() {
    return auth.currentUser;
  }

  signInWithGoogle(BuildContext context) async {
    final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
    final GoogleSignIn _googleSignIn = GoogleSignIn();

    final GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();

    final GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;
  }
}

the await _googleSignIn.signIn(); part creates a problem saying A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount'.

This kind of thing happens every time in many parts of the code even though the tutorial I'm following doesn't get this kind of problem at all.

CodePudding user response:

If I understand your problem correctly, it is not an await/async problem.

The return type of _googleSignIn.signIn() is not GoogleSignInAccount. Instead it is optional return type.

The only thing you have to do now is to support optional values.

I just rewrite it for you:

final GoogleSignInAccount? googleSignInAccount = await _googleSignIn.signIn();

if (googleSignInAccount != null) {
    final GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;
}
  • Related