0

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.

Eza
  • 3
  • 2
  • see https://dart.dev/codelabs/null-safety – pskink Aug 11 '22 at 06:35
  • That tutorial is pre-null safety. You can choose to run your app without null safety if you want it to work as is. The link in the previous comment should help you with all that. – Gazihan Alankus Aug 11 '22 at 06:45
  • Does this answer your question? [Turn off Null Safety for previous Flutter Project?](https://stackoverflow.com/questions/65302065/turn-off-null-safety-for-previous-flutter-project) – Gazihan Alankus Aug 11 '22 at 06:46
  • 1
    This was a breaking change in Flutter 2. Your tutorial is outdated. Flutter 2 came out 17 months ago, look for tutorials that are more recent than that. It is next to impossible to learn anything from a tutorial that in itself is no longer correct, because if you were able to spot the mistakes on your own, you would not need that tutorial. – nvoigt Aug 11 '22 at 07:09
  • 1
    @nvoigt So that's why.... thank you for your advice – Eza Aug 11 '22 at 07:13

1 Answers1

2

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;
}
Hannes
  • 86
  • 2