Reputation: 73
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
Upvotes: 4
Views: 988
Reputation: 1110
It is not a direct answer in the way that we both want it to be possible, and not 100% ideal because it is a package, but I found a way that we can find out which flavour was built. Using the package
import 'package:package_info_plus/package_info_plus.dart';
We can see the bundle identifier (i.e. com.example.app.prod, com.example.app.dev) and therefore we can decide which configuration to load. If dev
and prod
are build flavours then they will be appended to the end of the packageName
.
if (Platform.isAndroid) {
PackageInfo.fromPlatform().then((value) {
if (value.packageName == "com.example.app.dev") {
// load dev config
} else if (value.packageName == "com.example.app.prod") {
// load prod config
}
});
}
Upvotes: 0