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

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(...)

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 know if flutter app is in production mode?

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.

Is it possible to get information from a terminal into a flutter app? If yes, please share an example

I'm writing a flutter app that needs to set up elasticsearch on first run. To do this, I need to run some commands in the terminal, I know how to do it, but I also need information from the terminal for a complete setup. How to get information from terminal to my flutter app, can i assign the value of one of the terminal strings to some variable in flutter?
I not found a some issues about getting info from terminal to app.
You can use --dart-define from the terminal while running your app or while building your application.
for example
flutter run --dart-define=BASE_URL=https://flutter.dev
Here BASE_URL is a key and you can pass value to it, you can name it as per your need.
And, You can access this in your Flutter app like below:
const String.fromEnvironment('BASE_URL')
You can check out this video as well for the same: Passing values from the command line to Flutter app
const is required while accessing that value you pass using dart-define and the key name is case sensitive.

What should I use for i18n in Flutter: S.of(context) or S.current?

I'm using the i18n plugin for Flutter (I believe it's this one) that comes with Android Studio.
And in every example I see it says to use S.of(context).my_string to get the Strings but it always returns null.
If I use S.current.my_string, it seems to work.
So is S.current the right way to do it and every doc/tutorial out there is wrong, are they the same or what?
What I'm basically asking here, is what is the difference between them.
Seems like S.of(context) is initially available way to access localised string.
But sometimes you need to use it without Build Context (in ViewModel, for example). So S.current was added for these cases.
More info here

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.