'How to check if google user login is new account in flutter

How to check if the user that login with google is new or existed. If new I want to navigate to other screen. If not I want to navigate to home screen.

  loginGoogle() async {
String retVal = "error";
try {
  final GoogleSignInAccount? googleUser = await googleSignin.signIn();
  final GoogleSignInAuthentication? googleAuth =
      await googleUser?.authentication;
  final AuthCredential credential = GoogleAuthProvider.credential(
      idToken: googleAuth?.idToken, accessToken: googleAuth?.accessToken);
  UserCredential _authResult =
      await authService.signInWithCredential(credential);
  if (_authResult.additionalUserInfo!.isNewUser) {
    UserModel _user = UserModel(
      uid: _authResult.user!.uid,
      email: _authResult.user!.email!,
      fullName: _authResult.user!.displayName!,
      accountCreated: Timestamp.now(),
      provider: 'Google',
    );
    String _returnString = await Database().createUser(_user);
    if (_returnString == "success") {
      retVal = "success";
    }
  }
  retVal = "success";
} on PlatformException catch (e) {
  retVal = e.message!;
} catch (e) {
  // ignore: avoid_print
  print(e);
}

return retVal;

}

Code from sign up Screen

    Padding(
          padding: const EdgeInsets.only(top: 20.0),
          child: SignInButton(Buttons.Google,
              onPressed: () => authBloc.loginGoogle()),
        ),


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source