cast_ nullable_ to_ non_ nullable
Don't cast a nullable value to a non nullable type.
Details
#DON'T cast a nullable value to a non nullable type. This hides a null check and most of the time it is not what is expected.
BAD:
class A {}
class B extends A {}
A? a;
var v = a as B;
var v = a as A;
GOOD:
class A {}
class B extends A {}
A? a;
var v = a! as B;
var v = a!;
Enable
#
To enable the
cast_nullable_to_non_nullable
rule, add
cast_nullable_to_non_nullable
under
linter > rules
in your
analysis_options.yaml
file:
linter:
rules:
- cast_nullable_to_non_nullable
If you're instead using the YAML map syntax to configure linter rules,
add
cast_nullable_to_non_nullable: true
under
linter > rules:
linter:
rules:
cast_nullable_to_non_nullable: true
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Report an issue.