do-not-use-environment

Same as do_not_use_environment in Dart lints

Group
Style
Path
lint/style/do-not-use-environment
Default
Off, opt in
Origin
Dart lints
Rule
226 of 512

Disallow reading compilation environment declarations, including type aliases.

Reports invocations of bool.fromEnvironment, int.fromEnvironment, String.fromEnvironment, and bool.hasEnvironment. Constructor identity is resolved through type aliases and import prefixes; dot-shorthand invocations are checked using their expected type. User-defined constructors with the same names and constructor tear-offs are not reported.

Invalid

example.dartdart
import 'dart:core';
import 'dart:core' as core;

const bool debug = bool.fromEnvironment('DEBUG');
const int port = int.fromEnvironment('PORT', defaultValue: 8080);
const String flavor = String.fromEnvironment('FLAVOR');
const bool hasFlavor = bool.hasEnvironment('FLAVOR');
final bool runtimeDebug = new bool.fromEnvironment('DEBUG');
const prefixed = core.String.fromEnvironment('NAME');
const explicitConst = const int.fromEnvironment('LEVEL');
const bool shorthand = .fromEnvironment('SHORT');

class Config {
  static const String apiKey = String.fromEnvironment('API_KEY');
  final int retries = .fromEnvironment('RETRIES');
}

void main() {
  const bool local = bool.fromEnvironment('LOCAL');
  print([local, debug, port, flavor, hasFlavor, runtimeDebug, prefixed, explicitConst, shorthand]);
}
Avoid using environment declarations.
3
4const bool debug = bool.fromEnvironment('DEBUG');
Avoid using environment declarations.
4const bool debug = bool.fromEnvironment('DEBUG');
5const int port = int.fromEnvironment('PORT', defaultValue: 8080);
Avoid using environment declarations.
5const int port = int.fromEnvironment('PORT', defaultValue: 8080);
6const String flavor = String.fromEnvironment('FLAVOR');
Avoid using environment declarations.
6const String flavor = String.fromEnvironment('FLAVOR');
7const bool hasFlavor = bool.hasEnvironment('FLAVOR');
Avoid using environment declarations.
7const bool hasFlavor = bool.hasEnvironment('FLAVOR');
8final bool runtimeDebug = new bool.fromEnvironment('DEBUG');
Avoid using environment declarations.
8final bool runtimeDebug = new bool.fromEnvironment('DEBUG');
9const prefixed = core.String.fromEnvironment('NAME');
Avoid using environment declarations.
9const prefixed = core.String.fromEnvironment('NAME');
10const explicitConst = const int.fromEnvironment('LEVEL');
Avoid using environment declarations.
10const explicitConst = const int.fromEnvironment('LEVEL');
11const bool shorthand = .fromEnvironment('SHORT');
Avoid using environment declarations.
13class Config {
14 static const String apiKey = String.fromEnvironment('API_KEY');
Avoid using environment declarations.
14 static const String apiKey = String.fromEnvironment('API_KEY');
15 final int retries = .fromEnvironment('RETRIES');
Avoid using environment declarations.
18void main() {
19 const bool local = bool.fromEnvironment('LOCAL');

Valid

example.dartdart
import 'dart:io';

const bool debug = false;
const int port = 8080;
final String flavor = Platform.environment['FLAVOR'] ?? 'dev';
// A constructor tear-off is not an invocation.
final String Function(String, {String defaultValue}) reader = String.fromEnvironment;
// int.hasEnvironment / String.hasEnvironment do not exist; only bool's is checked.
final parsed = int.parse('42');
final text = String.fromCharCodes([72, 105]);

class Settings {
  const Settings.fromEnvironment();
  static const Settings settings = Settings.fromEnvironment();
}

void main() {
  print([debug, port, flavor, reader, parsed, text, Settings.settings]);
}

Configure do-not-use-environment

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "do-not-use-environment": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/do-not-use-environment: <reason>
  • double-literal-formatdouble-literal-format

    Flags double literals written with redundant or missing digits.

  • empty-constructor-bodiesempty_constructor_bodiesrecommended

    Flags constructors with an empty block body {}.

  • enum-constants-ordering

    Flags enum constants whose declared names violate the configured order.

  • eol-at-end-of-fileeol_at_end_of_filerecommended

    Flags a non-empty file that does not end with exactly one line break.

  • extend-equatableequatable

    Require Equatable semantics for configured class/file name patterns.

  • file-namesfile_namesrecommended

    Enforces lowercase_with_underscores for source file names.

  • flutter-style-todosflutter_style_todos

    Flags TODO comments that do not follow the Flutter style // TODO(username): message, https://URL-to-issue.

  • format-commentformat-comment

    Flags comments that are not formatted as complete sentences.