Contents

cast_nullable_to_non_nullable

Contents

Don't cast a nullable value to a non nullable type.

This rule is available as of Dart 2.12.

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:

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!;

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:

analysis_options.yaml
yaml
linter:
  rules:
    - cast_nullable_to_non_nullable