Setting environment variables in Flutter

For example, creating a client for an API, such as Twitch.

In the Dart CLI binary, I could use a shared environment variable or a Dart definition variable. For example, using as backups:

main() { String clientId = // dart -dCLIENT_ID='abc bin/example.dart // This is considered "compiled-into" the application. const String.fromEnvironment('CLIENT_ID') ?? // CLIENT_ID='abc' dart bin/example.dart // This is considered a runtime flag. Platform.environment['CLIENT_ID']; // Use clientId. } 

Does Flutter have a way to install both of them, in particular ...

  • During dev
  • When sending to prod

Help with some documents with pleasure as soon as I find out how :)

+12
dart flutter
source share
2 answers

For configuration, the general template that I saw is to use separate core files. those.

flutter run -t lib/production_main.dart

and

flutter build apk -t lib/debug_main.dart

And then in these different core files, configure the necessary configurations.

As for reading identifiers, you can do this from arbitrary resources https://flutter.io/assets-and-images/ .

I believe Flutter can be read from the environment as you suggest, however I don’t know how to set these environment variables on iOS or Android.

+6
source share

Since I also tried to solve this problem and ran into this thread, I just wanted to add this for people who are looking for a solution in the future ... There is currently a supported way to get information about whether the application is running or not:

 const bool isProduction = bool.fromEnvironment('dart.vm.product'); 

By the proposal:

https://twitter.com/FlutterDev/status/1048278525432791041

https://github.com/flutter/flutter/issues/4014

0
source share

All Articles