bool.fromEnvironment constructor Null safety

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

Returns the boolean value of the environment declaration name.

The boolean value of the declaration is true if the declared value is the string "true", and false if the value is "false".

In all other cases, including when there is no declaration for name, the result is the defaultValue.

The result is the same as would be returned by:

(const String.fromEnvironment(name) == "true")
    ? true
    : (const String.fromEnvironment(name) == "false")
        ? false
        : defaultValue

Example:

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

If you want to use a different truth-string than "true", you can use the String.fromEnvironment constructor directly:

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

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.

Implementation

// 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 bool.fromEnvironment(String name,
    {bool defaultValue = false});