Environment Variables in Dart
Environment variables are a universal mechanism for conveying configuration information to programs. Let’s look at how to set, get, and list environment variables in Dart.
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is null.
The list of keys in the environment will depend on your particular machine.
If we set BAR
in the environment first, the running program picks that value up.
Note that in Dart, we use the Platform.environment
map to interact with environment variables. Setting environment variables programmatically is not directly supported in Dart for security reasons, so we simulate it by modifying the Platform.environment
map. In a real-world scenario, environment variables are typically set outside the Dart program.