飞镖2:Future< void>之间的差异.和Future< Null>

问题描述:

具有不返回值的异步函数,理想的返回类型为Future<Null>Future<void>是什么?或更具体地说,使用这两种方法有什么区别?两者都是合法的,并且在两种情况下,函数的返回值都是解析为nullFuture.以下代码两次打印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));
}

类型Null仅允许值null

类型void允许使用任何类型的值,但要告知不应使用该值.

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

我尚不清楚工具支持如何处理void.可能会有林特规则提示或警告使用void值.

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代替了void,因为仅支持void作为方法/函数的返回类型.

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