问题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
foo instead. The difference between
() 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?
It's not passing junk, it's just not being used. 2)
null is a keyword and can't be used as a variable name. 3) Yes, It saves typing and also gives a low profile look. 4) No,
_ is an assigned variable. No surprises here.