问题描述
我正在学习Dart,并且经常看到以下成语:
I'm learning Dart and see the following idiom a lot:
someFuture.then((_)=> someFunc());
someFuture.then((_) => someFunc());
我也看到过类似的代码:
I have also seen code like:
someOtherFuture.then(()=> someOtherFunc ());
这两个示例之间是否存在功能差异?
Aka,将 _
作为Dart函数的参数传递有什么作用?
Is there a functional difference between these two examples?A.k.a., What does passing _
as a parameter to a Dart function do?
鉴于Dart的使用,这尤其令人困惑 _
作为声明私有功能的前缀。
This is particularly confusing given Dart's use of _
as a prefix for declaring private functions.
推荐答案
名为 _
的变量,通常是因为您计划不使用它并扔掉它。例如,您可以使用名称 x
或 foo
。
(_)
和()
之间的区别很简单,因为一个函数接受一个参数,而
It's a variable named _
typically because you plan to not use it and throw it away. For example you can use the name x
or foo
instead.The difference between (_)
and ()
is simple in that one function takes an argument and the other doesn't.
异常::未使用的参数可以命名为_,__,___等。此
发生在回调中,在该回调中您向其传递了值,但您却获得了
不需要使用它。给它起一个仅由
下划线组成的名称是表示不使用该值的惯用方式。
Exception: An unused parameter can be named _, __, ___, etc. Thishappens in things like callbacks where you are passed a value but youdon’t need to use it. Giving it a name that consists solely ofunderscores is the idiomatic way to indicate the value isn’t used.
这篇关于将`_`(即下划线)作为Dart语言函数的唯一参数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!