bool.fromEnvironment constructor

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

Boolean value for name in the compilation configuration environment.

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 string values can be directly accessed using String.fromEnvironment.

This constructor parses the string value associated with name as a boolean, as if by bool.tryParse, meaning that it accepts only the strings "true" and "false".

If there is no value associated with name in the compilation configuration environment, or if the associated string value is not one of "true" or "false", the value of the constructor invocation is the defaultValue boolean, which defaults to the boolean value false.

The result is the same as that of:

(const String.fromEnvironment(name) == "true")
    || ((const String.fromEnvironment(name) != "false") && defaultValue)


const bool loggingEnabled = bool.fromEnvironment("logging");

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

const bool? yesNoMaybe = bool.hasEnvironment("optionalFlag")
    ? bool.fromEnvironment("optionalFlag")
    : null;

To accept other strings than "true" or "false", use the String.fromEnvironment constructor directly. Example:

const isLoggingOn = (const String.fromEnvironment("logging") == "on");

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.


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