Adding firebase to flutter project

JaeHyun ShimJaeHyun Shim
3 min read

A problem I faced today was configuring Firebase onto a new Flutter project. My company is migrating an existing Android and iOS app into Flutter, and I needed to attach existing Firebase apps to the Flutter project. The main problem was that both native apps had multiple build variants and they did not share the same application id (bundle id). However, if you just follow the documentation, the flutterfire configure command detects the Flutter project id and tries to create new projects onto the Firebase.

Fortunately, flutterfire configure came with a help option that suggested that I could manually set the application id for Android and iOS, and if it matched the app's already existing in Firebase, the Flutter app would automatically be linked to them.

flutterfire configure --help
...

Configure Firebase for your Flutter app. This command will fetch Firebase configuration for you and generate a Dart file with prefilled FirebaseOptions you can use.

Usage: flutterfire configure [arguments]
-h, --help                                  Print this usage information.
-p, --project=<aliasOrProjectId>            The Firebase project to use for this command.
-e, --account=<email>                       The Google account to use for authorization.
-o, --out=<filePath>                        The output file path of the Dart file that will be generated with your Firebase configuration options.
                                            (defaults to "lib/firebase_options.dart")
-y, --yes                                   Skip the Y/n confirmation prompts and accept default options (such as detected platforms).
    --platforms=<platforms>                 Optionally specify the platforms to generate configuration options for as a comma separated list. For example "android,ios,macos,web,linux,windows".
-i, --ios-bundle-id=<bundleIdentifier>      The bundle identifier of your iOS app, e.g. "com.example.app". If no identifier is provided then an attempt will be made to automatically detect it from your "ios" folder (if it exists).
-m, --macos-bundle-id=<bundleIdentifier>    The bundle identifier of your macOS app, e.g. "com.example.app". If no identifier is provided then an attempt will be made to automatically detect it from your "macos" folder (if it exists).
    --android-app-id=<applicationId>        DEPRECATED - use "android-package-name" instead. The application id of your Android app, e.g. "com.example.app". If no identifier is provided then an attempt will be made to automatically detect it from your "android" folder (if it exists)
-a, --android-package-name=<packageName>    The package name of your Android app, e.g. "com.example.app". If no package name is provided then an attempt will be made to automatically detect it from your "android" folder (if it exists).
-w, --web-app-id=<appId>                    The app id of your Web application, e.g. "1:XXX:web:YYY". If no package name is provided then an attempt will be made to automatically pick the first available web app id from remote.
-t, --token=<firebaseToken>                 The token generated by running `firebase login:ci`

Run "flutterfire help" to see global options.

...
flutterfire configure -acom.packagename.app

Another solution would have been to go to Firebase and manually copy-paste the information to the firebase_options.dart file, but I opted for the automatic solution.

As for multiple build variants, I just separated them by what the build mode was and returned the respectable FirebaseOption in the firebase_options.dart file.

0
Subscribe to my newsletter

Read articles from JaeHyun Shim directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

JaeHyun Shim
JaeHyun Shim