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
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
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');
}
}final int? a = 3;final int? a = 3;const String? b = 'x';class C { static final int? sf = 5; void method() { final double? d = 1.5; final double? d = 1.5; final List<int>? list = [1, 2]; final List<int>? list = [1, 2]; final Map<String, int>? m = {'a': 1}; final Map<String, int>? m = {'a': 1}; final bool? flag = true;Valid
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.
{
"linter": {
"rules": {
"style": {
"unnecessary-nullable-for-final-variable-declarations": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/unnecessary-nullable-for-final-variable-declarations: <reason>More style rules
- unnecessary-parenthesis
unnecessary_parenthesisrecommendedFlags parentheses that can be removed without changing the meaning or hurting readability.
- unnecessary-raw-strings
unnecessary_raw_stringsrecommendedFlags raw string literals that contain no backslash or
$. - unnecessary-string-escapes
unnecessary_string_escapesrecommendedFlags backslashes that do not affect a string's value.
- unnecessary-string-interpolations
unnecessary_string_interpolationsrecommendedFlags a string literal whose entire content is a single interpolation of a provably non-nullable
String. - unnecessary-this
unnecessary_thisrecommendedFlags a redundant
this.qualifier on member access. - unreachable-from-main
unreachable_from_mainReport public declarations that no entry point of an executable library reaches.
- use-colored-box
use_colored_boxrecommendedflutterPrefers
ColoredBoxover aContainerthat only paints a color behind its child. - use-decorated-box
use_decorated_boxrecommendedflutterPrefers
DecoratedBoxover aContainerthat only decorates its child.