How to know if flutter app is in production mode? - flutter

I am configuring an API endpoint using the flutter package dio! I have two API endpoints: one for development and another for production. I want my dio baseURL to automatically switch between development and production endpoints.
Just like so
Dio(
baseURL: isProduction ? productionBaseURL : developmentBaseURL
......
);
How to know if my app is in production environment in Dart?

Just check the kReleaseMode global constant. It's true if the application was compiled in release mode.
Or, I'd recommend that this info comes from an environment variable so it follows one of the bullets from The Twelve-Factor App (the Config bullet). And to get it and check do the following.
For example, IS_PRODUCTION system environment variable:
final isProduction = Platform.environment['IS_PRODUCTION'] == '1';
Or, getting the dart-defines compile-time environment variables:
const isProduction = String.fromEnvironment('IS_PRODUCTION') == '1';
There are 2 ways to set a dart-define:
flutter run --dart-define=IS_PRODUCTION=1, or;
flutter build <bundle> --dart-define=IS_PRODUCTION=1;
Where <bundle> can be: aar, apk, appbundle, bundle, web, or windows.
Note that dart-define is compiled into the app itself. That's why they accept being const declaration. From the command-line docs it says:
Additional key-value pairs that will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors.

Use can use flavors in flutter to setup two different environments for your app, one for testing and one for production. You can refer this official documentation.

You have to decide and check yourself what "production" means in a way that suits your app. It can be a runtime setting for example in in shared_preferences, or it can be a compile-time setting you pass to flutter build.
I have implemented a pub.dev package firebase_options_selector that does this at runtime with a Firebase backend. You can use my source code as inspiration if you want.

Related

Is there a way to use Flutter Flavors and use that mechanism to set the --dart-define variables

With Flutter I use -dart-define in order set some environment variables for different API's I use in the flutter app. And I am able to retrieve them using String.fromEnvironment.
I also use Flavors in order to set different App icons for both IOS and Android.
This works fine but I end up with a very long build string e.g.
flutter build ipa --flavor=prod --dart-define BASE_URL=https://example-prod.com/ --dart-define API_PATH=/api/v2/ --dart-define OTHERVAR=blablabla --dart-define YETANOTHERVAR=blablablabla
What I am looking for is a way where I can type flutter build ipa --flavor-prod and a script or configuration setting (in gradle or xcconfig ... or another way) will also set the correct values for the --dart-define variables.
I've read a few articles and come across a few people asking the same thing, but I haven't found a proper answer. Most of them will answer that you can pick up the --dart-define values in the build script to get for example the AppName and use that in the build scripts, but I want it the other way around. e.g. If flavor=prod then -dart-define BASE_URL=https://example-prod.com

How to read `--flavor` parameter value from flutter code?

Flutter run and build commands accept the --flavor parameter. I have found a tutorial on implementing it in Android and iOS projects. And, I also found a way to access the flavor name from the flutter code.
But I still have no idea how to access the flavor name on other platforms. It seems flutter documentation has no information about it.
Is there a way to read the --flavor parameter value from the flutter code on any platform?
The reason for my question in the comments is that you might be looking for using the --dart-define flag when building your app, instead of --flavor.
They serve different purposes, so both might be required depending on your desired outcome.
The value passed via the dart define flag can be accessed in the code via String.fromEnvironment(...)

Flutter - how to create a framework where i can have Debug and Prod versions where debug version has some way to choose test environment

I need to create two different versions of the app.
- debug
-prod
for debug, version i need user to select the test environment before anything else begins. i need some way to know what environment user has selected and then load API endpoints config file accordingly.
what's the best way to handle this in flutter?
I have seen in some apps that for iOS, debug options are available in app settings under the standard iOS settings menu , select the app and then see those options in there.
You might want to use Flutter Flavours. As the name suggests, you can practically make flavours of the same app based on your need - debug, test, production etc. And not only API end-points, but you can also configure everything else such as app icon, different labels on the screen etc.
As suggested in this link, you can have different main.dart file for each flavour. You can read different configuration from JSON file (such as API end-point) and rest of your app will remain same. For example, see below :
This is another helpful link.

How do I get the proxy settings of an android/ios device using flutter?

I am trying to build an app using flutter and I would like to know how to retrieve the proxy settings of the device through flutter.
It's probably safe to assume that the HttpClient doesn't pick this up automatically, but you might want to test that.
So, now you need to interact with native code using plugins. There is already a rich library of plugins providing everything from battery level to video player. I can't see proxy in there anywhere, so you need to write your own plugin (which is just one of the standard flutter project types: app, package (just Dart code allowed) and plugin). A plugin is a bit like a package (other projects can depend on it) but includes native code too. It also includes a mini-app so that you can test your plugin code while developing it.
Your plugin will end up being similar to the existing Connectivity plugin, so you may want to copy from there. In your Android method implementation you will replace
NetworkInfo info = manager.getActiveNetworkInfo();
with
ProxyInfo defaultProxy = manager.getDefaultProxy();
You have return two values, the host name and the port, so put these in a Map
Map<String, String> map = new HashMap<String, String>();
map.put("host", defaultProxy.getHost());
map.put("port", Integer.toString(defaultProxy.getPort()));
result.success(map);
Bonus points if you submit your changes to the Connectivity plugin.

Passing command line arguments to a flutter app

Is package:args ArgParser compatible with flutter apps? I see on Github that it is used several times in some Flutter tools, but I'm not sure it's used in any of the sample apps.
If it is not compatible, is there another way to pass configuration options to my app at compile time as part of its build rule?
package:args operates on List<String>, which can come from anywhere. For example, I've used it in a browser app, in which the arguments came from Chrome's JS console. If you are OK with using the HostMessages API, then the following might work for you:
On Android, turn Intent.getExtras into List<String> and pass it to package:args. Similarly, this answer may help on the iOS side.