As i understand from google_maps_flutter docs we need to declare google maps api key inside AppDelegate.swift like this:

GMSServices.provideAPIKey("YOUR KEY HERE")

is there a way to use different api key for each flavor build? like for example different api key for dev and production


Solution 1: Dries Cruyskens

I used Active Compilation Conditions to solve this:

  1. In Xcode, go to PROJECT -> Runner -> Build Settings and search for 'active compilation conditions'. Add a text value for each different flavor. active compilation conditions

  2. in AppDelegate.swift, add preprocessor conditional statements to execute different code for different flavors, in our case providing a different API key:

    import UIKit
    import Flutter
    import GoogleMaps
    
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
    
        // Using active compilation conditions to provide the right API key for the right flavor.
        #if DEV
            GMSServices.provideAPIKey("<API key for DEV project>")
        #elseif QA
            GMSServices.provideAPIKey("<API key for QA project>")
        #elseif PROD
            GMSServices.provideAPIKey("<API key for PROD project>")
        #endif
    
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
    }