-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Different configurations for development and production #4014
Comments
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". |
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. |
You should be able to read environment variables from https://api.dartlang.org/1.14.0/dart-io/Platform/environment.html |
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. |
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 |
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. |
I spoke with @abarth just now, he suggested one workaround would be to use the -t argument to
|
That is reasonable. Thanks! |
@drewwarren did the approach work? |
Yup. 🍰 |
If launching a different main.dart works, then we shall close this bug! |
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! |
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. |
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 |
This now works
and should work well with tree-shaking. |
@ROTGP I like the demo application you developed. positives are:
|
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:
I 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/ |
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. |
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 |
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.
The text was updated successfully, but these errors were encountered: