Flutter のプロジェクトで
import Flutter
import UIKit
import GoogleMaps
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GMSServices.provideAPIKey(ProcessInfo.processInfo.environment["MY_API_KEY"]!)
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
のエラーが出ました。
この際直打ちのAPIキーはやめて、環境変数に設置することにしました。
やり方
XCodeでプロジェクト名のところをクリックすると Edit Scheme… という項目があるのでクリックします。


Environment Variables のところに + ボタンを押して、追加します。
GOOGLE_MAPS_API_KEY
hogehogehogehogehogehogehogeho
みたいな感じで Name, Value に設定しました。
その後、AppDelegate .swiftを以下のように書き換えました。
import Flutter
import UIKit
import GoogleMaps
@main
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Xcode の環境変数から APIキー を取得
if let apiKey = ProcessInfo.processInfo.environment["GOOGLE_MAPS_API_KEY"], !apiKey.isEmpty {
GMSServices.provideAPIKey(apiKey)
} else {
fatalError("Google Maps API Key is missing. Please set GOOGLE_MAPS_API_KEY in Xcode's Environment Variables.")
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
これで動作しました!やったー
コメントを残す