You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Different configurations for development and production #4014
What is the best way to have different configurations of a flutter application?
When developing my application, or creating builds for internal use, I want my app to make http requests against a development server. When I create a build for end users I want the http requests to go to a production server.
Could you possibly add a flag to flutter build/run that would pass supplied arguments to the main() function of our applications when they start up?
Although flutter might not need to have an implicit opinion on building environments I feel the current tooling prevents us from coming up with good solutions on our own.
Some options that I don't like but could work with the current tooling:
Maintaining separate branches of our application for different environments
Couldn't actually work for my team given our version control infrastructure.
Writing a "production" build script that swaps out or alters files before building.
It is possible there is a sane option with the current tooling I am not aware of.
I can read from Platform.environment, but it doesn't allow me to accomplish what I want. I don't want to change something on the phone to enable dev/production of my app; I want that to be baked into the .apk.
Looking at what some of these values look like on my android emulator:
Yeah, I'm suggesting that we provide an API on FlutterView (in Java) to supply an array of Strings that then populates the Platform.executableArguments (or some similar static) in Dart. That would let you send whatever configuration you wanted on startup.
I spoke with @abarth just now, he suggested one workaround would be to use the -t argument to flutter build to pass a different main.dart for your different versions.
fluter build apk -t lib/production_main.dart for example.
Quick question about this issue: When I use different "main.dart" files, how do I go about having different imports in different files.
For example: I have an import "settings.dart" which gives me a "const url" for a production or development endpoint depending on the given "main.dart".
How can I have it deeper down my application, in say "auth.dart"?
There is no way to get different imports depending on what the entry file is. You would need to instantiate different classes with the same interface in the main.dart file and pass them to the location where they are used.
There is a feature "configurable imports" in the Dart pipeline since quite some time that might land eventually that could help here. I wouldn't bet this lands the way you need it during 2018.
I'm not sure if any progress has been made on this, but I've put together a sample app demonstrating how this can be done (using custom entry point)... https://github.com/ROTGP/flutter_environments
Reacted by Byron Mackay, Erick Heredia and Harsh Bhikadia
Reacted by Chance Snow, thanglong134, Rafael Almeida Barbosa, Jerry Zhou, Jefferson Carvalho, Matias de Andrea, Sasha Kalmikov, Osvaldo Matos-Junior and 337240552
Reacted by thanglong134, Jefferson Carvalho, Sasha Kalmikov, Reynaldo and shakshared
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
eseidelGoogle commentedon May 18, 2016
I don't think we have a recommended approach at this time. @DaveShuckerow has asked similar of us as well.
It's possible the correct answer is "flutter has no opinion".
drewwarren commentedon May 18, 2016
Could you possibly add a flag to flutter build/run that would pass supplied arguments to the main() function of our applications when they start up?
Although flutter might not need to have an implicit opinion on building environments I feel the current tooling prevents us from coming up with good solutions on our own.
Some options that I don't like but could work with the current tooling:
It is possible there is a sane option with the current tooling I am not aware of.
abarth commentedon May 18, 2016
You should be able to read environment variables from https://api.dartlang.org/1.14.0/dart-io/Platform/environment.html
abarth commentedon May 18, 2016
I think it would also be reasonable to populate https://api.dartlang.org/1.14.0/dart-io/Platform/executableArguments.html if we don't already.
drewwarren commentedon May 18, 2016
I can read from Platform.environment, but it doesn't allow me to accomplish what I want. I don't want to change something on the phone to enable dev/production of my app; I want that to be baked into the .apk.
Looking at what some of these values look like on my android emulator:
flutter run my-flag --my-flag -- --my-flagabarth commentedon May 18, 2016
Yeah, I'm suggesting that we provide an API on FlutterView (in Java) to supply an array of Strings that then populates the
Platform.executableArguments(or some similar static) in Dart. That would let you send whatever configuration you wanted on startup.sethladd commentedon May 18, 2016
We might also want to start putting useful values into the Dart "environment", and made available to accessors like https://api.dartlang.org/stable/1.16.0/dart-core/String/String.fromEnvironment.html
e.g.
flutter.modecould return "release" or "debug" fromString.fromEnvironmenteseidelGoogle commentedon May 25, 2016
I spoke with @abarth just now, he suggested one workaround would be to use the -t argument to
flutter buildto pass a differentmain.dartfor your different versions.fluter build apk -t lib/production_main.dartfor example.drewwarren commentedon May 25, 2016
That is reasonable. Thanks!
eseidelGoogle commentedon May 26, 2016
@drewwarren did the approach work?
1 remaining item
Hixie commentedon Oct 17, 2017
If launching a different main.dart works, then we shall close this bug!
btastic commentedon Jan 24, 2018
Quick question about this issue: When I use different "main.dart" files, how do I go about having different imports in different files.
For example: I have an import "settings.dart" which gives me a "const url" for a production or development endpoint depending on the given "main.dart".
How can I have it deeper down my application, in say "auth.dart"?
Thanks!
zoechi commentedon Jan 25, 2018
There is no way to get different imports depending on what the entry file is. You would need to instantiate different classes with the same interface in the main.dart file and pass them to the location where they are used.
There is a feature "configurable imports" in the Dart pipeline since quite some time that might land eventually that could help here. I wouldn't bet this lands the way you need it during 2018.
ROTGP commentedon Aug 24, 2018
I'm not sure if any progress has been made on this, but I've put together a sample app demonstrating how this can be done (using custom entry point)... https://github.com/ROTGP/flutter_environments
zoechi commentedon Oct 15, 2018
This now works
#18829 (comment)
and should work well with tree-shaking.
daadu commentedon Oct 22, 2019
@ROTGP I like the demo application you developed.
positives are:
jamesdixon commentedon Nov 11, 2019
After reading a bunch of threads on this, I ended up with the multiple entry point approach as mentioned earlier.
I ended up doing the following:
get_itbut could also easily be done viaprovideror another mechanismI went with JSON because I could then exclude the files from my git repo and then recreate them on the fly from env variables set in my CI system.
I wrote a brief article that outlines most of the process: https://flutterigniter.com/env-specific-configuration/
ch-muhammad-adil commentedon Sep 8, 2020
What i feel this answer is good but i have tried below solution and it has given me true when i run in release mode.
I suggest use of both solution or may be you can just use this above code and change your end point if it is in production.
Now there is no need of creating multiple main.dart files for production and development.
github-actions commentedon Aug 12, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.