Rolando Urquiza
Rolando Urquiza

Reputation: 1450

Dart 2: Difference between Future<void> and Future<Null>

Having an asynchronous function that doesn't return a value, what's the ideal return type Future<Null> or Future<void>?, or more specifically, what's the difference in using either? Both are legal, and in both cases the return value of the function is a Future that resolves to null. The following code prints null two times:

import 'dart:async';

Future<void> someAsync() async {}
Future<Null> otherAsync() async {}

main() {
    someAsync().then((v) => print(v));
    otherAsync().then((v) => print(v));
}

Upvotes: 17

Views: 3976

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657666

The type Null only allows the value null

The type void allows values of any type, but communicates that the value shouldn't be used.

It's not yet clear to me how tools support will treat void. There will probably linter rules that hint or warn at using void values.

Null was used instead of void previously because void was only supported as return type of methods/functions.

Upvotes: 22

Related Questions