Rules / Correctness / proper-from-environment

proper-from-environment

lint/correctness/proper-from-environment
recommended
Same as proper_from_environment · Pyramid Lint

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);
}
*.fromEnvironment must be used in a const context, otherwise it returns the default at runtime.
1final apiUrl = String.fromEnvironment('API_URL');
*.fromEnvironment must be used in a const context, otherwise it returns the default at runtime.
2
3final debug = bool.fromEnvironment('DEBUG');
*.fromEnvironment must be used in a const context, otherwise it returns the default at runtime.
4
5final port = int.fromEnvironment('PORT');
*.fromEnvironment must be used in a const context, otherwise it returns the default at runtime.
6
7String read() => String.fromEnvironment('KEY');
*.fromEnvironment must be used 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');

How to configure

Set the severity of proper-from-environment in your falcon.json:

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