飞镖2:Future< void>之间的差异.和Future< Null> [英] Dart 2: Difference between Future<void> and Future<Null>

查看:135
本文介绍了飞镖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.

这篇关于飞镖2:Future&lt; void&gt;之间的差异.和Future&lt; Null&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆