dart flutter中函数参数(_)
来源:stackoverflow.com 更新时间:2023-05-25 21:55
问题1:I'm learning Dart and see the following idiom a lot:
someFuture.then((_) => someFunc());
I have also seen code like:
someOtherFuture.then(() => someOtherFunc());
Is there a functional difference between these two examples? A.k.a., What does passing _
as a parameter to a Dart function do?
This is particularly confusing given Dart's use of _
as a prefix for declaring private functions.
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.
问题2:So, the function being called expects a parameter, and the code is passing junk (an unassigned variable)? Why not use
null
, for clarity? Just to save typing? And, won't the function be a little surprised when it tries to use the unassigned, untyped variable? Or, am I missing something?