unnecessary-nullable-for-final-variable-declarations

Same as unnecessary_nullable_for_final_variable_declarations in Dart lints

Group
Style
Path
lint/style/unnecessary-nullable-for-final-variable-declarations
Default
On (recommended)
Origin
Dart lints
Rule
467 of 512
recommended

Flags a final or const variable given a nullable type but initialized to a provably non-null value.

A final int? x = 3; can never hold null: it is initialized once, at the declaration, to a value that is obviously non-null. The ? widens the static type for no reason, forcing needless null checks at every use. The rule is deliberately conservative about "provably non-null" — only literals (numbers, strings, booleans, collections, records), constructor invocations, and function expressions qualify; anything whose value flows from another binding is left alone. It applies to final/const top-level variables, static fields, and local variables. Drop the ? from the declared type.

Invalid

example.dartdart
final int? a = 3;
const String? b = 'x';

class C {
  static final int? sf = 5;

  void method() {
    final double? d = 1.5;
    final List<int>? list = [1, 2];
    final Map<String, int>? m = {'a': 1};
    final bool? flag = true;
    print('$d $list $m $flag');
  }
}
Remove the unnecessary nullable type from this final variable declaration.
1final int? a = 3;
Remove the unnecessary nullable type from this final variable declaration.
1final int? a = 3;
2const String? b = 'x';
Remove the unnecessary nullable type from this final variable declaration.
4class C {
5 static final int? sf = 5;
Remove the unnecessary nullable type from this final variable declaration.
7 void method() {
8 final double? d = 1.5;
Remove the unnecessary nullable type from this final variable declaration.
8 final double? d = 1.5;
9 final List<int>? list = [1, 2];
Remove the unnecessary nullable type from this final variable declaration.
9 final List<int>? list = [1, 2];
10 final Map<String, int>? m = {'a': 1};
Remove the unnecessary nullable type from this final variable declaration.
10 final Map<String, int>? m = {'a': 1};
11 final bool? flag = true;

Valid

example.dartdart
final int a = 3;
final int? nullable = maybeNull();
var x = 5;
int? topLevel = 3;

class C {
  final int? field = 3;
  int? notFinal;

  void method() {
    final int? fromCall = compute();
    final int? explicit = null;
    int? mutable = 3;
    print('$fromCall $explicit $mutable');
  }
}

// `second` needs the nullable type, so the shared type is not unnecessary.
final int? first = 1, second = maybeNull();

int? maybeNull() => null;
int? compute() => null;

Configure unnecessary-nullable-for-final-variable-declarations

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "unnecessary-nullable-for-final-variable-declarations": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/unnecessary-nullable-for-final-variable-declarations: <reason>
  • unnecessary-parenthesisunnecessary_parenthesisrecommended

    Flags parentheses that can be removed without changing the meaning or hurting readability.

  • unnecessary-raw-stringsunnecessary_raw_stringsrecommended

    Flags raw string literals that contain no backslash or $.

  • unnecessary-string-escapesunnecessary_string_escapesrecommended

    Flags backslashes that do not affect a string's value.

  • unnecessary-string-interpolationsunnecessary_string_interpolationsrecommended

    Flags a string literal whose entire content is a single interpolation of a provably non-nullable String.

  • unnecessary-thisunnecessary_thisrecommended

    Flags a redundant this. qualifier on member access.

  • unreachable-from-mainunreachable_from_main

    Report public declarations that no entry point of an executable library reaches.

  • use-colored-boxuse_colored_boxrecommendedflutter

    Prefers ColoredBox over a Container that only paints a color behind its child.

  • use-decorated-boxuse_decorated_boxrecommendedflutter

    Prefers DecoratedBox over a Container that only decorates its child.