prefer-void-to-null

Same as prefer_void_to_null in Dart lints

Path
lint/correctness/prefer-void-to-null
Default
On (recommended)
Origin
Dart lints
Rule
421 of 512
recommended

Flags uses of the Null type where void is meant.

Null and void both describe "no useful value", but void says the value must not be used while Null admits only null and is a subtype of every nullable type. Writing Future<Null> or a Null return type usually means void, and only void lets implementations return whatever they like.

Null f() {}                   // flagged
Future<Null> g() async {}     // flagged
void h() {}                   // preferred

Following the official lint, Null is accepted where void cannot replace it: casts (as Null, cast patterns), local and top-level variable types, the return and parameter types written directly in a function type, type arguments of empty collection literals (<Null>[]), extension on Null, extension type representations, and return types of members overriding a member that does not return void after substituting inherited type arguments. Overrides whose external declarations are unavailable are conservatively exempt; an unresolved source superclass is not evidence of an override.

Invalid

example.dartdart
Null f() {}

Future<Null> g() async {}

void h(Null x) {}

class A {
  Null field;

  Null method() => null;

  Stream<Null> get events => const Stream.empty();
}

void main() {
  final List<Null> items = <Null>[null];
  print(items);
}

class Base {
  void run() {}
}

class Sub extends Base {
  @override
  Null run() => null;
}

Map<String, Null> lookup() => {};

class Remote extends SomeUnknownBase {
  Null fetch() => null;
}
Avoid using the type `Null`.
1Null f() {}
Avoid using the type `Null`.
2
3Future<Null> g() async {}
Avoid using the type `Null`.
4
5void h(Null x) {}
Avoid using the type `Null`.
7class A {
8 Null field;
Avoid using the type `Null`.
9
10 Null method() => null;
Avoid using the type `Null`.
11
12 Stream<Null> get events => const Stream.empty();
Avoid using the type `Null`.
15void main() {
16 final List<Null> items = <Null>[null];
Avoid using the type `Null`.
15void main() {
16 final List<Null> items = <Null>[null];
Avoid using the type `Null`.
25 @override
26 Null run() => null;
Avoid using the type `Null`.
28
29Map<String, Null> lookup() => {};
Avoid using the type `Null`.
31class Remote extends SomeUnknownBase {
32 Null fetch() => null;

Valid

example.dartdart
void f() {}

Future<void> g() async {}

// Local and top-level variable types.
Null topLevel;

void locals(Object? o) {
  Null local;
  print(local);

  // Casts and cast patterns.
  final n = o as Null;
  print(n);
  if (o case final _ as Null) {}

  // Empty collection literals.
  final list = <Null>[];
  final map = <Null, Null>{};
  final set = <Null>{};
  print([list, map, set]);
}

// Types written directly in a function type.
Null Function(Null) callback = (_) => null;

// extension on Null.
extension on Null {
  bool get isNothing => true;
}

// Extension type representation.
extension type Nothing(Null value) {}

// Overriding a member that does not return void.
class Base {
  Object? value() => 1;
}

class Sub extends Base {
  @override
  Null value() => null;
}


// Overriding an SDK member.
class Failure extends Error {
  @override
  Null get stackTrace => null;
}

Configure prefer-void-to-null

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "prefer-void-to-null": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/correctness/prefer-void-to-null: <reason>
  • proper-controller-disposeproper_controller_disposerecommendedflutter

    Requires Flutter controllers owned by a State field to be disposed.

  • proper-expanded-and-flexibleproper_expanded_and_flexiblerecommendedflutter

    Require Expanded and Flexible to sit directly inside a flex widget.

  • proper-from-environmentproper_from_environmentrecommended

    Require fromEnvironment to be evaluated in a const context.

  • proper-super-init-stateproper_super_init_staterecommendedflutter

    Require super.initState() to be the first statement of a resolved Flutter State subclass's initState override.

  • require-super-disposerecommendedflutter

    Require super.dispose() in Flutter State dispose overrides.

  • sort-pub-dependenciessort_pub_dependenciescross-file

    Require pubspec.yaml dependency sections to be sorted alphabetically.

  • test-types-in-equalstest_types_in_equalsrecommended

    Flags casting the parameter of operator == instead of testing its type.

  • throw-in-finallythrow_in_finallyrecommended

    Flags throw inside a finally block.