Skip to content

Conversation

@stuartmorgan-g
Copy link
Collaborator

Converts the top-level plugin class and its supporting platform view factory from Objective-C to Swift. This changes very little implementation, but lays the groundwork for future incremental conversions.

Part of flutter/flutter#119108

Pre-Review Checklist

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. 2 3

Converts the top-level plugin class and its supporting platform view
factory from Objective-C to Swift. This changes very little
implementation, but lays the groundwork for future incremental
conversions.

Part of flutter/flutter#119108
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully converts the top-level plugin class and its associated platform view factory from Objective-C to Swift. The changes are well-contained and include necessary updates to the podspec, module definitions, and test files to support the new Swift implementation. The core logic, including the handling of shared GMSServices, is correctly preserved.

My review includes one suggestion to improve the robustness of the new Swift code by replacing force-unwrapping and force-casting with guard statements. This will make the plugin more resilient to unexpected inputs and prevent potential crashes.

Comment on lines 30 to 36
// Precache shared map services, if needed. Initializing this prepares GMSServices
// on a background thread controlled by the GoogleMaps framework.
let mapServices = GoogleMapFactory.sharedMapServices

return FLTGoogleMapController(
frame: frame, viewIdentifier: viewId,
creationParameters: args as! FGMPlatformMapViewCreationParams, registrar: registrar!)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of force casting (as!) and force unwrapping (!) can make the app crash if the assumptions about args type or registrar being non-nil are violated. It's safer to use guard let with conditional casting (as?) and optional binding to validate these before use. This provides more robust error handling and clearer failure messages if something goes wrong, which is especially useful for plugin consumers.

Also, using _ = ... is more idiomatic for calling something for its side effects while ignoring the result, which avoids an 'unused variable' warning.

    // Precache shared map services, if needed. Initializing this prepares GMSServices
    // on a background thread controlled by the GoogleMaps framework.
    _ = GoogleMapFactory.sharedMapServices

    guard let creationParams = args as? FGMPlatformMapViewCreationParams else {
      // This is a programmer error on the Dart side, so crashing is reasonable.
      fatalError("Invalid creation parameters for Google Map: \(String(describing: args))")
    }
    guard let registrar = self.registrar else {
      // This should not happen in a normal lifecycle.
      fatalError("Registrar is nil when creating Google Map.")
    }

    return FLTGoogleMapController(
      frame: frame, viewIdentifier: viewId,
      creationParameters: creationParams, registrar: registrar)

XCTAssertEqual(mapView.frameObserverCount, 0);
}

- (void)testMapsServiceSync {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is removed because it was testing that this code and its supported declarations were all wired up correctly, and in Swift having a lazy type (class-level) variable is a built-in language feature and all of that code is just this line now, so there's nothing meaningful to test now.

@stuartmorgan-g stuartmorgan-g marked this pull request as draft December 2, 2025 12:50
@stuartmorgan-g
Copy link
Collaborator Author

Well, this is fun. We appear to have a SwiftPM-style problem with CocoaPods as well if we adopt any Swift in the target:

Swift Compiler Error (Xcode): Compiling for iOS 14.0, but module 'GoogleMapsUtils' has a minimum deployment target of iOS 15.0:

It goes away if I change the podspec's s.platform = :ios, '14.0' to s.platform = :ios, '15.0', even though this is compiling the iOS 15 example app and thus shouldn't actually be compiling anything for iOS 14 regardless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant