What does it mean to pass `_` (i.e., underscore) as the sole parameter to a Dart language function?
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.
DON’T use a leading underscore for identifiers that aren’t private.
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
That expression is similar to "callbacks" in node.js, the expression have relation to async task.
First remember that => expr
expression is shorthand for {return *expr*}
, now in someFuture.then((_) => someFunc())
, someFuture
is a variable of type Future
, and this keeps your async task, with the .then
method you tell what to do with your async task (once completed), and args in this method you put the callback ((response) => doSomethingWith(response))
.
You learn more at Future-Based APIs and Functions in Dart. Thanks