prefer-void-to-null
Same as prefer_void_to_null in Dart lints
- Group
- Correctness
- Path
lint/correctness/prefer-void-to-null- Default
- On (recommended)
- Origin
- Dart lints
- Rule
- 421 of 512
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
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;
}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];void main() { final List<Null> items = <Null>[null]; @override Null run() => null; Map<String, Null> lookup() => {};class Remote extends SomeUnknownBase { Null fetch() => null;Valid
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.
{
"linter": {
"rules": {
"correctness": {
"prefer-void-to-null": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/prefer-void-to-null: <reason>More correctness rules
- proper-controller-dispose
proper_controller_disposerecommendedflutterRequires Flutter controllers owned by a
Statefield to be disposed. - proper-expanded-and-flexible
proper_expanded_and_flexiblerecommendedflutterRequire
ExpandedandFlexibleto sit directly inside a flex widget. - proper-from-environment
proper_from_environmentrecommendedRequire
fromEnvironmentto be evaluated in a const context. - proper-super-init-state
proper_super_init_staterecommendedflutterRequire
super.initState()to be the first statement of a resolved FlutterStatesubclass'sinitStateoverride. - require-super-disposerecommendedflutter
Require
super.dispose()in Flutter State dispose overrides. - sort-pub-dependencies
sort_pub_dependenciescross-fileRequire
pubspec.yamldependency sections to be sorted alphabetically. - test-types-in-equals
test_types_in_equalsrecommendedFlags casting the parameter of
operator ==instead of testing its type. - throw-in-finally
throw_in_finallyrecommendedFlags
throwinside afinallyblock.