Closed12
Flutterで環境ごとにFirebase Projectを切り替える方法

flutter_flavorizrでうまく読み込みがうまく行かなかったので、手動で切り替える旧来の方法を(雑に)まとめる

日本語の記事
- https://qiita.com/tokkun5552/items/cd70c65dd06935d8c751
- https://medium.com/flutter-jp/flavor-b952f2d05b5d
- https://blog.covelline.com/entry/2021/05/03/175755
- https://zuma-lab.com/posts/flutter-develop-staging-production-ios-environment
英語の記事
- https://medium.com/@animeshjain/build-flavors-in-flutter-android-and-ios-with-different-firebase-projects-per-flavor-27c5c5dac10b
- https://codewithandrea.com/articles/flutter-flavors-for-firebase-apps/
- https://sebastien-arbogast.com/2022/05/02/multi-environment-flutter-projects-with-flavors/#Creating_Schemes

全て同じワークアラウンドかわからないので、上記の記事を見て最小公倍数的なやり方を取りたい

丁寧なのでこの記事も追加

環境の整理
ビルドモードとFlavorは別。
自分の場合は以下のような感じかな。
用途 | ビルドモード | Flavor | Configuraton名 | Product Bundle Identifier |
---|---|---|---|---|
ローカル用 | Debug | local | Debug-local | xxx.yyy.zzz.local |
開発用 | Debug | dev | Debug-dev | xxx.yyy.zzz.dev |
本番用 | Release | prod | Release-prod | xxx.yyy.zzz |
Configuration名は何に使うんだろう?

Schemeの追加
Xcode Menu > Product > Scheme > Manage Schemes
から環境に必要なFlavorを作成
- Target: Runner
- Name: Development

Configuration の追加
Runner > PROJECT > Runner のConfigurationを以下の様に変更
変更前
- Debug
- Release
- Profile
変更後
- Debug
- Release
- Profile
- Debug-dev
- Debug-local
- Release-prod

xcconfigの追加
Runner > Flutter 配下にて、ファイル一覧から New File > Configuration Settings Fileとして、xcconfigファイルの作成
- Debug.xcconfig
- Release.xcconfig
- Profile.xcconfig
- Debug-v.xcconfig
- Debug-local.xcconfig
- Release-prod.xcconfig

Info.plistの対応だけで済むかと思ったけど、調べていくと、flutterのfirebaseのライブラリの初期化時に必要なので、結局FlutterFireのコードも使うことになりそう
Firebase.initializeApp()
で以下の様な例外を吐く
FirebaseException ([core/not-initialized] Firebase has not been correctly initialized.
Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`.
View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization
)

FlutterFireで生成される firebase_app_id_file.json
はcrashlyticsを使わなければ必要ない

flutterfireで環境を分けるためには以下の様なコマンドを打つ必要がある
flutterfire configure -i "your-bundle-id" -a "your-package-name" -o
lib/firebase/firebase_options_dev.dart" - android_service "android/app/dev/google-services.json" -iso_service "ios/dev/firebase_app_id_file.json"

この記事がflutter_fireコマンドのflavor対応の仕方など含めて書いてある
(公式からリンクされている)
このスクラップは2024/08/16にクローズされました