Skip to content

Commit e113013

Browse files
authored
[google_sign_in] Migrate to nnbd (flutter#3329)
1 parent eccc3cd commit e113013

File tree

17 files changed

+150
-151
lines changed

17 files changed

+150
-151
lines changed

packages/google_sign_in/google_sign_in/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.0-nullsafety
2+
3+
* Migrate to nnbd.
4+
15
## 4.5.9
26

37
* Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets.

packages/google_sign_in/google_sign_in/integration_test/google_sign_in_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @dart = 2.9
2+
13
import 'package:integration_test/integration_test.dart';
24
import 'package:flutter_test/flutter_test.dart';
35
import 'package:google_sign_in/google_sign_in.dart';

packages/google_sign_in/google_sign_in/lib/google_sign_in.dart

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class GoogleSignInAuthentication {
2222
final GoogleSignInTokenData _data;
2323

2424
/// An OpenID Connect ID token that identifies the user.
25-
String get idToken => _data.idToken;
25+
String? get idToken => _data.idToken;
2626

2727
/// The OAuth2 access token to access Google services.
28-
String get accessToken => _data.accessToken;
28+
String? get accessToken => _data.accessToken;
2929

3030
/// Server auth code used to access Google Login
31-
String get serverAuthCode => _data.serverAuthCode;
31+
String? get serverAuthCode => _data.serverAuthCode;
3232

3333
@override
3434
String toString() => 'GoogleSignInAuthentication:$_data';
@@ -57,7 +57,7 @@ class GoogleSignInAccount implements GoogleIdentity {
5757
static const String kUserRecoverableAuthError = 'user_recoverable_auth';
5858

5959
@override
60-
final String displayName;
60+
final String? displayName;
6161

6262
@override
6363
final String email;
@@ -66,9 +66,9 @@ class GoogleSignInAccount implements GoogleIdentity {
6666
final String id;
6767

6868
@override
69-
final String photoUrl;
69+
final String? photoUrl;
7070

71-
final String _idToken;
71+
final String? _idToken;
7272
final GoogleSignIn _googleSignIn;
7373

7474
/// Retrieve [GoogleSignInAuthentication] for this account.
@@ -105,7 +105,7 @@ class GoogleSignInAccount implements GoogleIdentity {
105105
///
106106
/// See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization.
107107
Future<Map<String, String>> get authHeaders async {
108-
final String token = (await authentication).accessToken;
108+
final String? token = (await authentication).accessToken;
109109
return <String, String>{
110110
"Authorization": "Bearer $token",
111111
"X-Goog-AuthUser": "0",
@@ -117,7 +117,7 @@ class GoogleSignInAccount implements GoogleIdentity {
117117
/// If client runs into 401 errors using a token, it is expected to call
118118
/// this method and grab `authHeaders` once again.
119119
Future<void> clearAuthCache() async {
120-
final String token = (await authentication).accessToken;
120+
final String token = (await authentication).accessToken!;
121121
await GoogleSignInPlatform.instance.clearAuthCache(token: token);
122122
}
123123

@@ -174,7 +174,7 @@ class GoogleSignIn {
174174
/// Factory for creating default sign in user experience.
175175
factory GoogleSignIn.standard({
176176
List<String> scopes = const <String>[],
177-
String hostedDomain,
177+
String? hostedDomain,
178178
}) {
179179
return GoogleSignIn(
180180
signInOption: SignInOption.standard,
@@ -212,22 +212,22 @@ class GoogleSignIn {
212212
final List<String> scopes;
213213

214214
/// Domain to restrict sign-in to.
215-
final String hostedDomain;
215+
final String? hostedDomain;
216216

217217
/// Client ID being used to connect to google sign-in. Only supported on web.
218-
final String clientId;
218+
final String? clientId;
219219

220-
StreamController<GoogleSignInAccount> _currentUserController =
221-
StreamController<GoogleSignInAccount>.broadcast();
220+
StreamController<GoogleSignInAccount?> _currentUserController =
221+
StreamController<GoogleSignInAccount?>.broadcast();
222222

223223
/// Subscribe to this stream to be notified when the current user changes.
224-
Stream<GoogleSignInAccount> get onCurrentUserChanged =>
224+
Stream<GoogleSignInAccount?> get onCurrentUserChanged =>
225225
_currentUserController.stream;
226226

227227
// Future that completes when we've finished calling `init` on the native side
228-
Future<void> _initialization;
228+
Future<void>? _initialization;
229229

230-
Future<GoogleSignInAccount> _callMethod(Function method) async {
230+
Future<GoogleSignInAccount?> _callMethod(Function method) async {
231231
await _ensureInitialized();
232232

233233
final dynamic response = await method();
@@ -237,7 +237,7 @@ class GoogleSignIn {
237237
: null);
238238
}
239239

240-
GoogleSignInAccount _setCurrentUser(GoogleSignInAccount currentUser) {
240+
GoogleSignInAccount? _setCurrentUser(GoogleSignInAccount? currentUser) {
241241
if (currentUser != _currentUser) {
242242
_currentUser = currentUser;
243243
_currentUserController.add(_currentUser);
@@ -258,7 +258,7 @@ class GoogleSignIn {
258258
}
259259

260260
/// The most recently scheduled method call.
261-
Future<void> _lastMethodCall;
261+
Future<void>? _lastMethodCall;
262262

263263
/// Returns a [Future] that completes with a success after [future], whether
264264
/// it completed with a value or an error.
@@ -279,15 +279,15 @@ class GoogleSignIn {
279279
/// The optional, named parameter [canSkipCall] lets the plugin know that the
280280
/// method call may be skipped, if there's already [_currentUser] information.
281281
/// This is used from the [signIn] and [signInSilently] methods.
282-
Future<GoogleSignInAccount> _addMethodCall(
282+
Future<GoogleSignInAccount?> _addMethodCall(
283283
Function method, {
284284
bool canSkipCall = false,
285285
}) async {
286-
Future<GoogleSignInAccount> response;
286+
Future<GoogleSignInAccount?> response;
287287
if (_lastMethodCall == null) {
288288
response = _callMethod(method);
289289
} else {
290-
response = _lastMethodCall.then((_) {
290+
response = _lastMethodCall!.then((_) {
291291
// If after the last completed call `currentUser` is not `null` and requested
292292
// method can be skipped (`canSkipCall`), re-use the same authenticated user
293293
// instead of making extra call to the native side.
@@ -303,8 +303,8 @@ class GoogleSignIn {
303303
}
304304

305305
/// The currently signed in account, or null if the user is signed out.
306-
GoogleSignInAccount get currentUser => _currentUser;
307-
GoogleSignInAccount _currentUser;
306+
GoogleSignInAccount? get currentUser => _currentUser;
307+
GoogleSignInAccount? _currentUser;
308308

309309
/// Attempts to sign in a previously authenticated user without interaction.
310310
///
@@ -323,7 +323,7 @@ class GoogleSignIn {
323323
/// one of [kSignInRequiredError] (when there is no authenticated user) ,
324324
/// [kNetworkError] (when a network error occurred) or [kSignInFailedError]
325325
/// (when an unknown error occurred).
326-
Future<GoogleSignInAccount> signInSilently({
326+
Future<GoogleSignInAccount?> signInSilently({
327327
bool suppressErrors = true,
328328
}) async {
329329
try {
@@ -354,21 +354,21 @@ class GoogleSignIn {
354354
/// a Future which resolves to the same user instance.
355355
///
356356
/// Re-authentication can be triggered only after [signOut] or [disconnect].
357-
Future<GoogleSignInAccount> signIn() {
358-
final Future<GoogleSignInAccount> result =
357+
Future<GoogleSignInAccount?> signIn() {
358+
final Future<GoogleSignInAccount?> result =
359359
_addMethodCall(GoogleSignInPlatform.instance.signIn, canSkipCall: true);
360360
bool isCanceled(dynamic error) =>
361361
error is PlatformException && error.code == kSignInCanceledError;
362362
return result.catchError((dynamic _) => null, test: isCanceled);
363363
}
364364

365365
/// Marks current user as being in the signed out state.
366-
Future<GoogleSignInAccount> signOut() =>
366+
Future<GoogleSignInAccount?> signOut() =>
367367
_addMethodCall(GoogleSignInPlatform.instance.signOut);
368368

369369
/// Disconnects the current user from the app and revokes previous
370370
/// authentication.
371-
Future<GoogleSignInAccount> disconnect() =>
371+
Future<GoogleSignInAccount?> disconnect() =>
372372
_addMethodCall(GoogleSignInPlatform.instance.disconnect);
373373

374374
/// Requests the user grants additional Oauth [scopes].

packages/google_sign_in/google_sign_in/lib/src/common.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ abstract class GoogleIdentity {
2828
/// The display name of the signed in user.
2929
///
3030
/// Not guaranteed to be present for all users, even when configured.
31-
String get displayName;
31+
String? get displayName;
3232

3333
/// The photo url of the signed in user if the user has a profile picture.
3434
///
3535
/// Not guaranteed to be present for all users, even when configured.
36-
String get photoUrl;
36+
String? get photoUrl;
3737
}

packages/google_sign_in/google_sign_in/lib/testing.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FakeSignInBackend {
3232
/// This does not represent the signed-in user, but rather an object that will
3333
/// be returned when [GoogleSignIn.signIn] or [GoogleSignIn.signInSilently] is
3434
/// called.
35-
FakeUser user;
35+
late FakeUser user;
3636

3737
/// Handles method calls that would normally be sent to the native backend.
3838
/// Returns with the expected values based on the current [user].
@@ -42,7 +42,7 @@ class FakeSignInBackend {
4242
// do nothing
4343
return null;
4444
case 'getTokens':
45-
return <String, String>{
45+
return <String, String?>{
4646
'idToken': user.idToken,
4747
'accessToken': user.accessToken,
4848
};
@@ -72,24 +72,24 @@ class FakeUser {
7272
});
7373

7474
/// Will be converted into [GoogleSignInUserData.id].
75-
final String id;
75+
final String? id;
7676

7777
/// Will be converted into [GoogleSignInUserData.email].
78-
final String email;
78+
final String? email;
7979

8080
/// Will be converted into [GoogleSignInUserData.displayName].
81-
final String displayName;
81+
final String? displayName;
8282

8383
/// Will be converted into [GoogleSignInUserData.photoUrl].
84-
final String photoUrl;
84+
final String? photoUrl;
8585

8686
/// Will be converted into [GoogleSignInTokenData.idToken].
87-
final String idToken;
87+
final String? idToken;
8888

8989
/// Will be converted into [GoogleSignInTokenData.accessToken].
90-
final String accessToken;
90+
final String? accessToken;
9191

92-
Map<String, String> get _asMap => <String, String>{
92+
Map<String, String?> get _asMap => <String, String?>{
9393
'id': id,
9494
'email': email,
9595
'displayName': displayName,

packages/google_sign_in/google_sign_in/lib/widgets.dart

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import 'dart:typed_data';
66

7-
import 'package:flutter/foundation.dart';
87
import 'package:flutter/material.dart';
98

109
import 'src/common.dart';
@@ -23,7 +22,7 @@ class GoogleUserCircleAvatar extends StatelessWidget {
2322
/// in place of a profile photo, or a default profile photo if the user's
2423
/// identity does not specify a `displayName`.
2524
const GoogleUserCircleAvatar({
26-
@required this.identity,
25+
required this.identity,
2726
this.placeholderPhotoUrl,
2827
this.foregroundColor,
2928
this.backgroundColor,
@@ -42,13 +41,13 @@ class GoogleUserCircleAvatar extends StatelessWidget {
4241
/// The color of the text to be displayed if photo is not available.
4342
///
4443
/// If a foreground color is not specified, the theme's text color is used.
45-
final Color foregroundColor;
44+
final Color? foregroundColor;
4645

4746
/// The color with which to fill the circle. Changing the background color
4847
/// will cause the avatar to animate to the new color.
4948
///
5049
/// If a background color is not specified, the theme's primary color is used.
51-
final Color backgroundColor;
50+
final Color? backgroundColor;
5251

5352
/// The URL of a photo to use if the user's [identity] does not specify a
5453
/// `photoUrl`.
@@ -57,7 +56,7 @@ class GoogleUserCircleAvatar extends StatelessWidget {
5756
/// then this widget will attempt to display the user's first initial as
5857
/// determined from the identity's [displayName] field. If that is `null` a
5958
/// default (generic) Google profile photo will be displayed.
60-
final String placeholderPhotoUrl;
59+
final String? placeholderPhotoUrl;
6160

6261
@override
6362
Widget build(BuildContext context) {
@@ -68,46 +67,34 @@ class GoogleUserCircleAvatar extends StatelessWidget {
6867
);
6968
}
7069

71-
/// Adds correct sizing information to [photoUrl].
72-
///
73-
/// Falls back to the default profile photo if [photoUrl] is [null].
74-
static String _sizedProfileImageUrl(String photoUrl, double size) {
75-
if (photoUrl == null) {
76-
// If the user has no profile photo and no display name, fall back to
77-
// the default profile photo as a last resort.
78-
return 'https://lh3.googleusercontent.com/a/default-user=s${size.round()}-c';
79-
}
80-
return fife.addSizeDirectiveToUrl(photoUrl, size);
81-
}
82-
8370
Widget _buildClippedImage(BuildContext context, BoxConstraints constraints) {
8471
assert(constraints.maxWidth == constraints.maxHeight);
8572

8673
// Placeholder to use when there is no photo URL, and while the photo is
8774
// loading. Uses the first character of the display name (if it has one),
8875
// or the first letter of the email address if it does not.
89-
final List<String> placeholderCharSources = <String>[
76+
final List<String?> placeholderCharSources = <String?>[
9077
identity.displayName,
9178
identity.email,
9279
'-',
9380
];
9481
final String placeholderChar = placeholderCharSources
95-
.firstWhere((String str) => str != null && str.trimLeft().isNotEmpty)
82+
.firstWhere((String? str) => str != null && str.trimLeft().isNotEmpty)!
9683
.trimLeft()[0]
9784
.toUpperCase();
9885
final Widget placeholder = Center(
9986
child: Text(placeholderChar, textAlign: TextAlign.center),
10087
);
10188

102-
final String photoUrl = identity.photoUrl ?? placeholderPhotoUrl;
89+
final String? photoUrl = identity.photoUrl ?? placeholderPhotoUrl;
10390
if (photoUrl == null) {
10491
return placeholder;
10592
}
10693

10794
// Add a sizing directive to the profile photo URL.
10895
final double size =
10996
MediaQuery.of(context).devicePixelRatio * constraints.maxWidth;
110-
final String sizedPhotoUrl = _sizedProfileImageUrl(photoUrl, size);
97+
final String sizedPhotoUrl = fife.addSizeDirectiveToUrl(photoUrl, size);
11198

11299
// Fade the photo in over the top of the placeholder.
113100
return SizedBox(

packages/google_sign_in/google_sign_in/pubspec.yaml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_sign_in
22
description: Flutter plugin for Google Sign-In, a secure authentication system
33
for signing in with a Google account on Android and iOS.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in
5-
version: 4.5.9
5+
version: 5.0.0-nullsafety
66

77
flutter:
88
plugin:
@@ -16,27 +16,21 @@ flutter:
1616
default_package: google_sign_in_web
1717

1818
dependencies:
19-
google_sign_in_platform_interface: ^1.1.1
19+
google_sign_in_platform_interface: ^2.0.0-nullsafety
2020
flutter:
2121
sdk: flutter
22-
meta: ^1.0.4
23-
# The design on https://flutter.dev/go/federated-plugins was to leave
24-
# this constraint as "any". We cannot do it right now as it fails pub publish
25-
# validation, so we set a ^ constraint.
26-
# TODO(amirh): Revisit this (either update this part in the design or the pub tool).
27-
# https://github.com/flutter/flutter/issues/46264
28-
google_sign_in_web: ^0.9.1
22+
meta: ^1.3.0-nullsafety.6
2923

3024
dev_dependencies:
3125
http: ^0.12.0
3226
flutter_driver:
3327
sdk: flutter
3428
flutter_test:
3529
sdk: flutter
36-
pedantic: ^1.8.0
30+
pedantic: ^1.10.0-nullsafety.1
3731
integration_test:
3832
path: ../../integration_test
3933

4034
environment:
41-
sdk: ">=2.1.0 <3.0.0"
42-
flutter: ">=1.12.13+hotfix.4"
35+
sdk: ">=2.12.0-0 <3.0.0"
36+
flutter: ">=1.12.13+hotfix.5"

0 commit comments

Comments
 (0)