飞镖2:Future< void>之间的差异.和Future< Null> [英] Dart 2: Difference between Future<void> and Future<Null>
问题描述
具有不返回值的异步函数,理想的返回类型为Future<Null>
或Future<void>
是什么?或更具体地说,使用这两种方法有什么区别?两者都是合法的,并且在两种情况下,函数的返回值都是解析为null
的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));
}
推荐答案
类型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.
这篇关于飞镖2:Future< void>之间的差异.和Future< Null>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!