21
votes

I am trying to figure out how to set up different firebase environments in a flutter project.

I understand how to do this in firebase, I created two projects, one for production, one for the test. Then, in an iOS or Android project, I could use various methods to switch between these two environments using separate google-services.json or GoogleServices-Info.plist files.

In Flutter I found this description of how to separate environments, but it only explains how to differentiate between environments in the flutter code.

How can I get this environment to change what iOS and Android build at compile time? It would even be sufficient simply to allow a file copy hook at build time.

5

5 Answers

2
votes

You can switch accounts using FirebaseApp.configure. You can offer your own solution or secret dev panel to switch between them.

The solutions will build flavours and plist implementations will lock you into builds when you deploy for TestFlight + they are messy.

Here's an example: (You could use Assets as well.)

// Load a named file.
let filePath = Bundle.main.path(forResource: "MyGoogleService", ofType: "plist")
guard let fileopts = FirebaseOptions(contentsOfFile: filePath!)
  else { assert(false, "Couldn't load config file") }
FirebaseApp.configure(options: fileopts)
1
votes

Salvatore Giordano has written a blog post with a detailed description of how to achieve this:

https://medium.com/@salvatoregiordanoo/flavoring-flutter-392aaa875f36

Flutter accepts a parameter --flavor=<flavor> which allows you to select different build flavors. In Android this works as expected, selecting different build flavors. IOS is a little tricker because a scheme is needed for every flavor, and the build configurations in the form of Release-<flavor> are also needed.

Once these parts are in place, they can be used, to select the firebase configuration as you would in any iOS or Android project.

The challenge is getting Dart code to also be aware of the flavor, and the blog post provides no good solution for this. It suggests the standard method of using different entry points can be used, but the correct entry point must be matched to the correct flavor manually by the person invoking the app.

1
votes

I wrote an article about how to do this for Firebase configuration as well as runtime configuration in dart code using flavors and platform channels.

https://medium.com/@matt.goodson.business/separating-build-environment-configurations-in-flutter-with-firebase-doing-it-the-right-way-c72c3ad3621f

Flutter flavors work pretty seamlessly with Android flavors. For iOS you need to create Xcode schemes for each flavor and link them to build configurations.

For dart configuration, you can use platform channels to get the flavor used during the build at runtime. This lets you configure the app without having multiple main.dart files or passing a target argument.

-1
votes

Specifically to Firebase env config you can use this article and this article from CodeMagic which explains how you can set up plist files with build env variables.

If you need to have a different set of values inside your Dart code, like an option you can use this package. It allows to generate Dart class config file from console command params.

Update 12/05/2020

Since Flutter 1.17 you can actually use compile-time variables with --dart-define argument in flutter run and flutter build commands

Here is an article that describes how to specify and use them.