String.fromEnvironment constructor

const String.fromEnvironment(
  1. String name, {
  2. String defaultValue = "",
})

Value for name in the compilation configuration environment declaration.

The compilation configuration environment is provided by the surrounding tools which are compiling or running the Dart program. The environment is a mapping from a set of string keys to their associated string value. The string value, or lack of a value, associated with a name must be consistent across all calls to String.fromEnvironment, int.fromEnvironment, bool.fromEnvironment and bool.hasEnvironment in a single program.

The result of invoking this constructor is the string associated with the key name. If no value is associated with name, the result is instead the defaultValue string, which defaults to the empty string.

Example of looking up a value:

const String.fromEnvironment("defaultFloo", defaultValue: "no floo")

In order to check whether a value is there at all, use bool.hasEnvironment. Example:

const maybeDeclared = bool.hasEnvironment("maybeDeclared")
    ? String.fromEnvironment("maybeDeclared")
    : null;

The string value, or lack of a value, associated with a name must be consistent across all calls to String.fromEnvironment, int.fromEnvironment, bool.fromEnvironment and bool.hasEnvironment in a single program.

This constructor is only guaranteed to work when invoked as const. It may work as a non-constant invocation on some platforms which have access to compiler options at run-time, but most ahead-of-time compiled platforms will not have this information.

The compilation configuration environment is not the same as the environment variables of a POSIX process. Those can be accessed on native platforms using Platform.environment from the dart:io library.

Implementation

external const factory String.fromEnvironment(
  String name, {
  String defaultValue = "",
});