I have a Google Sign In Flow in my new Flutter App. (First Flutter App). If cancel is pressed, the app crashes. From what I have found, the easiest solution is to wrap the sign in flow in a try-catch statement.
This works fine in debug mode. The error is caught and the circle and the view is reset to how it was from start. When a sign in process starts I have a circle
But when I run in release mode it just crashes. I have tried some different versions but none have worked.
@override
Widget build(BuildContext context) {
if (user != null) {
return MaterialApp(
home: Scaffold(
backgroundColor: backgroundColor,
body: loadDataAndGoToMain(),
),
);
}
double deviceWidth = MediaQuery.of(context).size.width;
return WillPopScope(
onWillPop: () async => false,
child: Scaffold(
backgroundColor: backgroundColor,
body: SizedBox(
width: deviceWidth,
child: _isLoading
? const Center(child: CircularProgressIndicator())
: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
backgroundImage(),
if (Platform.isIOS) ...{
FractionallySizedBox(
widthFactor: 0.85, child: appleButton(context)),
},
FractionallySizedBox(
widthFactor: 0.85, child: googleButton(context)), <-- Problem
FractionallySizedBox(
widthFactor: 0.85,
child: TextButton(
style: getStyle(Colors.white),
onPressed: () {
toggleState();
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return const MainView();
},
),
);
},
child: const Text("Continue Offline"),
),
)
],
),
),
),
);
}
Google button:
Widget googleButton(BuildContext context) {
return SignInButton(
Buttons.Google,
text: "Sign in with Google",
onPressed: () async {
toggleState();
startGoogleSignIn();
},
);
}
startGoogleSignIn:
Future<void> startGoogleSignIn() async {
try {
final user = await signInWithGoogle();
await loadUserData(user!);
Navigator.of(context).push<void>(
MaterialPageRoute<void>(
builder: (BuildContext context) {
return const MainView();
},
),
);
} on PlatformException { <- I tried this just because it did not work with one catch.
toggleState();
} catch (error) {
toggleState();
}
}
signInWithGoogle:
Future<User?> signInWithGoogle() async {
// Trigger the authentication flow
final GoogleSignInAccount? googleUser;
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
final authResult =
await FirebaseAuth.instance.signInWithCredential(credential);
return authResult.user;
}