将`_`(即下划线)作为Dart语言函数的唯一参数是什么意思? [英] What does it mean to pass `_` (i.e., underscore) as the sole parameter to a Dart language function?

查看:582
本文介绍了将`_`(即下划线)作为Dart语言函数的唯一参数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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. This happens in things like callbacks where you are passed a value but you don’t need to use it. Giving it a name that consists solely of underscores is the idiomatic way to indicate the value isn’t used.

https://dart.dev/guides/language/effective-dart/style

这篇关于将`_`(即下划线)作为Dart语言函数的唯一参数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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