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:
dart
class A {}
class B extends A {}
A? a;
var v = a as B;
var v = a as A;
GOOD:
dart
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:
analysis_options.yaml
yaml
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:
analysis_options.yaml
yaml
linter:
rules:
cast_nullable_to_non_nullable: true
Unless stated otherwise, the documentation on this site reflects Dart 3.6.2. Page last updated on 2025-01-27. View source or report an issue.