proper-from-environment

Same as proper_from_environment in Pyramid Lint

Path
lint/correctness/proper-from-environment
Default
On (recommended)
Origin
Pyramid Lint
Rule
424 of 512
recommended

Require fromEnvironment to be evaluated in a const context.

Flags a String.fromEnvironment, int.fromEnvironment, or bool.fromEnvironment invocation that is not in a const context. These constructors read --dart-define values only when evaluated at compile time; called at runtime they ignore the environment entirely and silently return the default, so a missing const turns a configured value into its fallback with no error. Prefix the call with const, or place it somewhere already const — a const variable initializer or an annotation argument.

Invalid

example.dartdart
final apiUrl = String.fromEnvironment('API_URL');

final debug = bool.fromEnvironment('DEBUG');

final port = int.fromEnvironment('PORT');

String read() => String.fromEnvironment('KEY');

void f() {
  var mode = String.fromEnvironment('MODE');
  print(mode);
}
Use `*.fromEnvironment` in a `const` context; otherwise it returns the default at runtime.
1final apiUrl = String.fromEnvironment('API_URL');
Use `*.fromEnvironment` in a `const` context; otherwise it returns the default at runtime.
2
3final debug = bool.fromEnvironment('DEBUG');
Use `*.fromEnvironment` in a `const` context; otherwise it returns the default at runtime.
4
5final port = int.fromEnvironment('PORT');
Use `*.fromEnvironment` in a `const` context; otherwise it returns the default at runtime.
6
7String read() => String.fromEnvironment('KEY');
Use `*.fromEnvironment` in a `const` context; otherwise it returns the default at runtime.
9void f() {
10 var mode = String.fromEnvironment('MODE');

Valid

example.dartdart
const apiUrl = String.fromEnvironment('API_URL');

const debug = bool.fromEnvironment('DEBUG');

const port = int.fromEnvironment('PORT');

class C {
  static const key = String.fromEnvironment('KEY');
}

const list = [
  String.fromEnvironment('A'),
  String.fromEnvironment('B'),
];

String get env => const String.fromEnvironment('ENV');

class EnvironmentValue {
  final String value;
  const EnvironmentValue(this.value);
}

@EnvironmentValue(String.fromEnvironment('ANNOTATED'))
void annotated() {}

Configure proper-from-environment

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "proper-from-environment": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/proper-from-environment: <reason>
  • proper-super-init-stateproper_super_init_staterecommendedflutter

    Require super.initState() to be the first statement of a resolved Flutter State subclass's initState override.

  • require-super-disposerecommendedflutter

    Require super.dispose() in Flutter State dispose overrides.

  • sort-pub-dependenciessort_pub_dependenciescross-file

    Require pubspec.yaml dependency sections to be sorted alphabetically.

  • test-types-in-equalstest_types_in_equalsrecommended

    Flags casting the parameter of operator == instead of testing its type.

  • throw-in-finallythrow_in_finallyrecommended

    Flags throw inside a finally block.

  • type-literal-in-constant-patterntype_literal_in_constant_patternrecommended

    Don't use constant patterns whose expressions have the core Type type.

  • undeclared-dependenciescross-file

    Report package imports and exports missing from the enclosing pubspec.

  • unnecessary-flutter-importsrecommendedflutter

    Disallow unused Flutter framework and dart:async imports.