String.fromEnvironment constructor Null safety

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

The string value of the environment declaration name.

Environment declarations are provided by the surrounding system compiling or running the Dart program. Declarations map a string key to a string value.

If name is not declared in the environment, the result is instead defaultValue.

Example of getting a value:

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

In order to check whether a declaration 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 .fromEnvironment() constructors are special in that we do not want
// users to call them using "new". We prohibit that by giving them bodies
// that throw, even though const constructors are not allowed to have bodies.
// Disable those static errors.
//ignore: const_constructor_with_body
//ignore: const_factory
external const factory String.fromEnvironment(String name,
    {String defaultValue = ""});