I can’t figure out how to use an environment variable indicating dev or prod environment to automatically choose which firebase project to connect to with my Flutter mobile app.
I am a newbie developer trying to develop an app with Flutter and Firebase’s storage service. I am currently trying to set up a development workflow that would allow me to make changes in a dev environment, then push the changes to my production server.
I first saw this Firebase documentation on working with different environments in a development workflow, which explicitly recommended using different Firebase projects for each environments:
Key Point: We recommend reading our guides thoroughly, but here’s the top takeaway about development workflows:
Firebase recommends using a separate Firebase project for each environment in your development workflow.
So I went ahead and made a new Firebase project, but then had trouble connecting to it. After using
flutterfire configure and selecting my new development project, my app still communicated with my original project intended for production.
I tried following this thread to make 2 copies of my
firebase_options.dart file and choose which one to use in my
void main() function, but that made no difference. I also didn’t see how the solution dealt with the other configuration files that
flutterfire configure added.
Eventually, I noticed that one configuration file mentioned in this documentation was not being changed by the
flutterfire configure command:
GoogleServices-Info.plist, which was apparently used to connect to the correct Firebase app when running on iOS. So I had to choose which version of that file (and the other configuration files) to use based on my environment variable (indicating whether I was in a dev or prod environment)
For that, I found this documentation which I really should have read earlier, about how to use multiple Firebase apps in one project. It mentions that
Examples of when it can be useful to use multiple Firebase projects include:
Setting up your development environment to use different Firebase projects based on build type or target.
So it seemed pretty appropriate to my needs. Furthermore, it had sections detailing using of different environments in Android and Apple apps, and mentions two of the other configuration files that were previously unaccounted for:
firebase_app_id_file.json, the last configuration file added by
flutterfire configure, which does not seem to be referenced anywhere, whether in documentations or in the codebase of my project).
However, the instructions for using different versions of these files were sparse and cryptic to a new developer like me.
For the Apple section, I tried to follow the instructions to create a new
Target in my Xcode project, but had no idea how to select which one to use based on the environment variable I was using to differentiate between my dev and prod environment.
I found this thread for using multiple
Targets in an iOS app, but the answers given felt convoluted and hacky to me. I also still didn’t see how those solutions can make use of the aforementioned environment variables to automatically choose the Xcode
Target to use.
The alternative solution wasn’t any better. Under “Support multiple environments in your Apple application”, instructions are given to “choose at runtime which plist to load”, and an example is given. However, to an inexperienced developer like myself, I have no idea where in my code is the example supposed to be used, or even what language it is in.
For the Android section, methods to use build flavours were described, which I had yet to try. It seemed quite similar to the Xcode
Target for Apple apps though, which I fear would have similar issues of sparse references.
The alternative method was just as cryptic as the one in the Apple section. It said that I can “add the string resources directly to your app instead of using the Google Services grade plugin”, but no instructions are given whatsoever on how to do that.
It feels as though I am unintentionally trying to re-invent the wheel, or use an unnecessarily complicated method to achieve what I want. Surely it is a common need to have separate environments for development, production and any other stage in a development workflow, for there to be a well-documented method to do just that? What am I missing?