diff --git a/packages/google_sign_in/google_sign_in/CHANGELOG.md b/packages/google_sign_in/google_sign_in/CHANGELOG.md index e0212458a549..7bdee4ecdc5a 100644 --- a/packages/google_sign_in/google_sign_in/CHANGELOG.md +++ b/packages/google_sign_in/google_sign_in/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.5.4 + +* Fix `PlatformException` leaking in debug mode. + ## 4.5.3 * Update package:e2e -> package:integration_test diff --git a/packages/google_sign_in/google_sign_in/lib/google_sign_in.dart b/packages/google_sign_in/google_sign_in/lib/google_sign_in.dart index 0f1f15bbb8c4..78697676cdcd 100644 --- a/packages/google_sign_in/google_sign_in/lib/google_sign_in.dart +++ b/packages/google_sign_in/google_sign_in/lib/google_sign_in.dart @@ -245,16 +245,18 @@ class GoogleSignIn { return _currentUser; } - Future _ensureInitialized() { - return _initialization ??= GoogleSignInPlatform.instance.init( - signInOption: signInOption, - scopes: scopes, - hostedDomain: hostedDomain, - clientId: clientId, - )..catchError((dynamic _) { - // Invalidate initialization if it errors out. - _initialization = null; - }); + Future _ensureInitialized() async { + try { + return _initialization ??= GoogleSignInPlatform.instance.init( + signInOption: signInOption, + scopes: scopes, + hostedDomain: hostedDomain, + clientId: clientId, + ); + } catch (_) { + // Invalidate initialization if it errors out. + _initialization = null; + } } /// The most recently scheduled method call. @@ -354,12 +356,19 @@ class GoogleSignIn { /// a Future which resolves to the same user instance. /// /// Re-authentication can be triggered only after [signOut] or [disconnect]. - Future signIn() { - final Future result = - _addMethodCall(GoogleSignInPlatform.instance.signIn, canSkipCall: true); - bool isCanceled(dynamic error) => - error is PlatformException && error.code == kSignInCanceledError; - return result.catchError((dynamic _) => null, test: isCanceled); + Future signIn() async { + try { + return await _addMethodCall(GoogleSignInPlatform.instance.signIn, + canSkipCall: true); + } catch (e) { + bool isCanceled(dynamic error) => + error is PlatformException && error.code == kSignInCanceledError; + + if (isCanceled(e)) { + return null; + } + rethrow; + } } /// Marks current user as being in the signed out state. diff --git a/packages/google_sign_in/google_sign_in/pubspec.yaml b/packages/google_sign_in/google_sign_in/pubspec.yaml index c5bc15f037c5..2b7aea7cc95c 100644 --- a/packages/google_sign_in/google_sign_in/pubspec.yaml +++ b/packages/google_sign_in/google_sign_in/pubspec.yaml @@ -2,7 +2,7 @@ name: google_sign_in description: Flutter plugin for Google Sign-In, a secure authentication system for signing in with a Google account on Android and iOS. homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in -version: 4.5.3 +version: 4.5.4 flutter: plugin: diff --git a/packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart b/packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart index 5969edbaba76..d917aa0f6228 100755 --- a/packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart +++ b/packages/google_sign_in/google_sign_in/test/google_sign_in_test.dart @@ -103,6 +103,13 @@ void main() { ]); }); + test('signIn canceled returns a null user', () async { + channel.setMockMethodCallHandler((MethodCall methodCall) { + throw PlatformException(code: GoogleSignIn.kSignInCanceledError); + }); + expect(await googleSignIn.signIn(), isNull); // should not throw + }); + test('disconnect; null response', () async { await googleSignIn.disconnect(); expect(googleSignIn.currentUser, isNull); @@ -307,7 +314,7 @@ void main() { throw "I am an error"; }); expect(googleSignIn.signInSilently(suppressErrors: false), - throwsA(isInstanceOf())); + throwsA(isA())); }); test('can sign in after init failed before', () async { @@ -321,8 +328,8 @@ void main() { } return Future.value(responses[methodCall.method]); }); - expect(googleSignIn.signIn(), throwsA(isInstanceOf())); - expect(await googleSignIn.signIn(), isNotNull); + expect(googleSignIn.signIn(), throwsA(isA())); + expect(await googleSignIn.signIn(), isA()); }); test('created with standard factory uses correct options', () async {