cast_nullable_to_non_nullable
Don’t cast a nullable value to a non nullable type.
This rule is available as of Dart 2.12.0.
This rule has a quick fix available.
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!;
Usage
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