There is a function
Future<void> refresh({bool value = false}) async {
changeUsers(await _service.loadUsers());
if (value) return;
...
}
in view I can use it the way
return RefreshIndicator(
onRefresh: refresh,
...
but if I need to pass named parameter
return RefreshIndicator(
onRefresh: refresh(value: true),
...
it gives the error
The argument type ‘Future’ can’t be assigned to the parameter
type ‘Future Function()’.
How to fix it?
2
Answers
To fix this error, you need to pass a function to the
onRefresh
parameter ofRefreshIndicator
. Currently, you are calling therefresh
function and passing its result toonRefresh
, which is causing the error.To pass a named parameter to the
refresh function
, you can use a lambda function or an anonymous function.onRefresh
onRefreshIndicator
takes a function with the signature:But when you provide
refresh(value: true)
, you are no longer providing a method of that signature but are instead calling yourrefresh
function and provides the result of that call which ends up beingFuture<void>
.If you want to call your method with some specific arguments, you need to provide a function to
onRefresh
which does that. E.g.:We are here providing
() => refresh(value: true)
as anonymous function which callsrefresh(value: true)
and returns whatever that call might return which here isFuture<void>
. Therefore this provided method fits the specified signature foronRefresh
.