An error occurs when Google Sign in canceled - flutter

When user pushes a button, signInWithGoogle() will be called.
Future<void> signInWithGoogle() async {
final GoogleSignInAccount? googleUser;
final GoogleSignInAuthentication googleAuth;
googleUser = await GoogleSignIn().signIn().catchError((e) {
context.read<LoginModel>().load(false); //Have the user login again
});
if (googleUser == null) {
return;
}
googleAuth = await googleUser.authentication;
final OAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await FirebaseAuth.instance.signInWithCredential(credential);
mainpage.accountCreated();
context.read<mainpage.UserInfo>().setUser();
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Success"),
));
context.read<mainpage.UserInfo>().getUserInfo();
}
If signin succeeded, no error has occurs.
However if the process canceled, the error below has occured.
throw PlatformException(code: errorCode, message: errorMessage as String?, details: errorDetails, stacktrace: errorStacktrace);
What should I do?

Related

Flutter Platform Exception in Google Sign

I have successfully completed the Google sign-in thing in Flutter. But if the user quits logging in I get an error. I couldn't figure out how to check for these errors. The answers I found are very old. Here is my code.
Future<UserCredential> signInWithGoogle() async {
final GoogleSignInAccount? googleUser =
await GoogleSignIn(scopes: <String>["email"]).signIn();
final GoogleSignInAuthentication googleAuth =
await googleUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
return await FirebaseAuth.instance.signInWithCredential(credential);
}
And my error
Exception has occurred.
PlatformException (PlatformException(sign_in_canceled, com.google.GIDSignIn, The user canceled the sign-in flow., null))
How can i handle that?

Flutter While trying to login to Google, the permissions in scopes continue unintentionally

While trying to login to Google, the permissions in scopes continue unintentionally.
Moreover:
W/System (11494): Ignoring header X-Firebase-Locale because its value was null.
W/System (11494): Ignoring header X-Firebase-Locale because its value was null.
I'm getting a warning like this in logcat.
final googleSignIn = GoogleSignIn(scopes: [
"https://www.googleapis.com/auth/user.gender.read",
"https://www.googleapis.com/auth/user.birthday.read"
]);
GoogleSignInAccount? _user;
GoogleSignInAccount get user => _user!;
Future googleLogin() async {
try {
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
await getGender();
await getBirthday();
await saveData();
await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
print(e.toString());
}
}
I get this error when I delete the app from the emulator and install it.
Future logout() async {
await googleSignIn.disconnect();
FirebaseAuth.instance.signOut();
}
After logging in once,
I do not encounter this error again after logging out with the above function
What could be the reason?

Flutter Google Sign In error after canceling Choose an account pop-up

After clicking sign in, choose an account pop comes up but if the user pressed back or outside of the pop-up it throws an error.
Error -
Sign in class -
class GoogleSignInProvider extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
GoogleSignInAccount? _user;
GoogleSignInAccount get user => _user!;
Future googleLogin() async {
await googleSignIn.signOut();
final googleUser = await googleSignIn.signIn();
if (googleUser == null) return;
_user = googleUser;
final googleAuth = await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
await FirebaseAuth.instance.signInWithCredential(credential);
notifyListeners();
}
}
Sign out button -
TextButton(
onPressed: () async {
final googleCurrentUser = GoogleSignIn().currentUser;
if (googleCurrentUser != null) {
await GoogleSignIn().disconnect();
}
FirebaseAuth.instance.signOut();
Navigator.pop(context);
},
child: const Text(
'Yes',
style: TextStyle(color: primaryColor),
))
Instead of if (googleUser == null) return; try using if (googleUser == null) return null;.
Here is my sign in method;
Future<UserCredential?> signInWithGoogle() async {
final _googleSignIn = GoogleSignIn();
await _googleSignIn.disconnect().catchError((e, stack) {
print(e);
});
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
// handling the exception when cancel sign in
if (googleUser == null) return null;
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth =
await googleUser.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
return await FirebaseAuth.instance.signInWithCredential(credential);
}
The sign out method;
Future signOut() async {
var result = await FirebaseAuth.instance.signOut();
return result;
}
This only happens in debugging, in release this won't be an issue.

How can i get the access token through google sign in -- Flutter

I want to get the access token from google sign and save it as a variable so I'll be able to push it to my own API , this question has been asked before but the answers are outdated . I'm using this package :Google sign in
her,s my log in code that i access through a button :
GestureDetector(
onTap: () {
_googleSignIn.signIn().catchError((e) {
print('Erorr');
});
;
},
To get access token when logged in with google use
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
Full method:
import 'package:google_sign_in/google_sign_in.dart';
Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;
// Create a new credential
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,
idToken: googleAuth?.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
for document https://firebase.flutter.dev/docs/auth/social/
Future<bool> google() async {
try {
final googleUser = await googleSignIn.signIn();
final googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth?.accessToken,// accessToken
idToken: googleAuth?.idToken,
);
users = (await auth.signInWithCredential(credential)).user;
if (users == null) {
return false;
}
return true;
} catch (e) {
print('this is error .......$e');
return null;
}
you can get the access token
googleAuth.accessToken //fellowing my code
Future<FirebaseUser> signInWithGoogle(SignInViewModel model) async {
model.state =ViewState.Busy;
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
AuthResult authResult = await _auth.signInWithCredential(credential);
_user = authResult.user;
assert(!_user.isAnonymous);
assert(await _user.getIdToken() != null);
FirebaseUser currentUser = await _auth.currentUser();
assert(_user.uid == currentUser.uid);
model.state =ViewState.Idle;
print("User Name: ${_user.displayName}");
print("User Email ${_user.email}");
}

flutter how to catch error google sign in

Future<UserCredential> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
}
Future signInWithFacebook() async {
// Trigger the sign-in flow
try {
final AccessToken accessToken = await FacebookAuth.instance.login();
// Create a credential from the access token
final OAuthCredential credential = FacebookAuthProvider.credential(
accessToken.token,
);
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
} on FacebookAuthException catch (e) {
// handle the FacebookAuthException
} on FirebaseAuthException catch (e) {
// handle the FirebaseAuthException
} finally {}
return null;
}
//pub.yaml
google_sign_in: ^5.0.1
make the social login auth but I can't catch error google sign in.
I'm already Facebook sign in and apple sign in error catch but
I can't catch error on google sign in
please help me
If you make the return type of <UserCredential?> nullable, you'd be able to work with it.
//this UserCredential?
Future<UserCredential?> signInWithGoogle() async {
// Trigger the authentication flow
try {
final GoogleSignInAccount? googleUser = await (GoogleSignIn().signIn());
// Obtain the auth details from the request
final GoogleSignInAuthentication googleAuth = await googleUser!.authentication;
// Create a new credential
final GoogleAuthCredential credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
) as GoogleAuthCredential;
// Once signed in, return the UserCredential
return await FirebaseAuth.instance.signInWithCredential(credential);
} catch (e) {
print(e);
return null ;
}
}
Right now I'm also facing this issue. Your code is fine. Run your app in Release mode and everything will work fine as expected. It's very strange behavior.
this only occurs in Emulators, not in the real devices.