Skip to content

Commit

Permalink
[google_sign_in_ios] Fix "callee requires a non-null parameter" analy…
Browse files Browse the repository at this point in the history
…zer warning (#7513)

The normal Cocoa pattern is to check a result, and only if it's nil then do something with the error.

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/CreateCustomizeNSError/CreateCustomizeNSError.html

> Important: Success or failure is indicated by the return value of the method. Although Cocoa methods that indirectly return error objects in the Cocoa error domain are guaranteed to return such objects if the method indicates failure by directly returning nil or NO, you should always check that the return value is nil or NO before attempting to do anything with the NSError object.

Use this pattern with the result and error from the sign in completion block.  This means `-didSignInForUser` is only called for non-nil users (the possibility of a nullable parameter was kicking off the analyzer warning).

![Screenshot 2024-08-26 at 4 24 04�PM](https://github.com/user-attachments/assets/56937344-ab54-4ac6-9f8c-7fdb84f9567a)

Fixes flutter/flutter#153587
  • Loading branch information
jmagman authored Aug 27, 2024
1 parent 1d1fc4f commit ce17b4b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
3 changes: 2 additions & 1 deletion packages/google_sign_in/google_sign_in_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 5.7.7

* Fixes "callee requires a non-null parameter" analyzer warning.
* Updates minimum supported SDK version to Flutter 3.19/Dart 3.3.

## 5.7.6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ - (void)signInSilentlyWithCompletion:(nonnull void (^)(FSIUserData *_Nullable,
FlutterError *_Nullable))completion {
[self.signIn restorePreviousSignInWithCompletion:^(GIDGoogleUser *_Nullable user,
NSError *_Nullable error) {
[self didSignInForUser:user withServerAuthCode:nil completion:completion error:error];
if (user != nil) {
[self didSignInForUser:user withServerAuthCode:nil completion:completion];
} else {
// Forward all errors and let Dart side decide how to handle.
completion(nil, getFlutterError(error));
}
}];
}

Expand Down Expand Up @@ -152,17 +157,14 @@ - (void)signInWithCompletion:(nonnull void (^)(FSIUserData *_Nullable,
[self signInWithHint:nil
additionalScopes:self.requestedScopes.allObjects
completion:^(GIDSignInResult *_Nullable signInResult, NSError *_Nullable error) {
GIDGoogleUser *user;
NSString *serverAuthCode;
if (signInResult) {
user = signInResult.user;
serverAuthCode = signInResult.serverAuthCode;
[self didSignInForUser:signInResult.user
withServerAuthCode:signInResult.serverAuthCode
completion:completion];
} else {
// Forward all errors and let Dart side decide how to handle.
completion(nil, getFlutterError(error));
}

[self didSignInForUser:user
withServerAuthCode:serverAuthCode
completion:completion
error:error];
}];
} @catch (NSException *e) {
completion(nil, [FlutterError errorWithCode:@"google_sign_in" message:e.reason details:e.name]);
Expand Down Expand Up @@ -291,30 +293,21 @@ - (GIDConfiguration *)configurationWithClientIdentifier:(NSString *)runtimeClien

- (void)didSignInForUser:(GIDGoogleUser *)user
withServerAuthCode:(NSString *_Nullable)serverAuthCode
completion:(nonnull void (^)(FSIUserData *_Nullable,
FlutterError *_Nullable))completion
error:(NSError *)error {
if (error != nil) {
// Forward all errors and let Dart side decide how to handle.
completion(nil, getFlutterError(error));
} else {
NSURL *photoUrl;
if (user.profile.hasImage) {
// Placeholder that will be replaced by on the Dart side based on screen size.
photoUrl = [user.profile imageURLWithDimension:1337];
}
NSString *idToken;
if (user.idToken) {
idToken = user.idToken.tokenString;
}
completion([FSIUserData makeWithDisplayName:user.profile.name
email:user.profile.email
userId:user.userID
photoUrl:[photoUrl absoluteString]
serverAuthCode:serverAuthCode
idToken:idToken],
nil);
completion:
(nonnull void (^)(FSIUserData *_Nullable, FlutterError *_Nullable))completion {
NSURL *photoUrl;
if (user.profile.hasImage) {
// Placeholder that will be replaced by on the Dart side based on screen size.
photoUrl = [user.profile imageURLWithDimension:1337];
}

completion([FSIUserData makeWithDisplayName:user.profile.name
email:user.profile.email
userId:user.userID
photoUrl:photoUrl.absoluteString
serverAuthCode:serverAuthCode
idToken:user.idToken.tokenString],
nil);
}

#if TARGET_OS_IOS
Expand Down
2 changes: 1 addition & 1 deletion packages/google_sign_in/google_sign_in_ios/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_sign_in_ios
description: iOS implementation of the google_sign_in plugin.
repository: https://github.com/flutter/packages/tree/main/packages/google_sign_in/google_sign_in_ios
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
version: 5.7.6
version: 5.7.7

environment:
sdk: ^3.3.0
Expand Down

0 comments on commit ce17b4b

Please sign in to comment.