1

This is my code:

// ignore: import_of_legacy_library_into_null_safe
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';

Future<Map<String, dynamic>> signInWithGoogle() async {
  final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );

  return {
    "email": googleUser.email,
    "photoUrl": googleUser.photoUrl,
    "credentials": await FirebaseAuth.instance.signInWithCredential(credential),
  };
}

Future<bool> signInWithEmail(String email, String password) async {
  try {
    FirebaseAuth.instance.
    signInWithEmailAndPassword(email: email, password: password);
    return true;
  } on FirebaseAuthException catch(e){
    print(e.code);
    return false;
  }
}



Future<bool> signUpWithEmail(String email, String password) async {
  try {
    FirebaseAuth.instance.
    createUserWithEmailAndPassword(email: email, password: password);
    return true;
  } on FirebaseAuthException catch(e){
    print(e.code);
    return false;
  }
}


This is the suggestion that flutter provides, but I don't know what to do:

A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount'. Try changing the type of the variable, or casting the right-hand type to 'GoogleSignInAccount'.

How can I solve this issue?

Thanks for any help you can provide

eamirho3ein
  • 16,619
  • 2
  • 12
  • 23
lomipac
  • 571
  • 8
  • 18
  • To start off, **where** does flutter appear to think the problem is? Also, **do you understand why** there is a problem? – Karl Knechtel Jan 02 '23 at 21:54
  • Hint: do you expect that `await GoogleSignIn().signIn();` will always give you an account? What if the credentials are wrong, for example? What do you think should happen in the rest of the code, in that case? – Karl Knechtel Jan 02 '23 at 21:55
  • 3
    Does this answer your question? [What is Null Safety in Dart?](https://stackoverflow.com/questions/60068435/what-is-null-safety-in-dart) – Karl Knechtel Jan 02 '23 at 21:56

1 Answers1

1

As the error said, GoogleSignIn().signIn() returns you GoogleSignInAccount? that means it could be null and you can't pass it to variable type GoogleSignInAccount. You need to change googleUser's type to GoogleSignInAccount?.

final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

after that you need to change your return map in signInWithGoogle to this:

final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;

...
return {
    "email": googleUser?.email ?? '',
    "photoUrl": googleUser?.photoUrl ?? '',
    "credentials": await FirebaseAuth.instance.signInWithCredential(credential),
  };

what I did is that use ? on googleUser, and set the default value (' ') incase that googleUser was null. you need to repeat this for credential too and provide it default value that good for it incase it get null too.

eamirho3ein
  • 16,619
  • 2
  • 12
  • 23