skip to Main Content

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


  1. To fix this error, you need to pass a function to the onRefresh parameter of RefreshIndicator. Currently, you are calling the refresh function and passing its result to onRefresh, which is causing the error.

    To pass a named parameter to the refresh function, you can use a lambda function or an anonymous function.

    return RefreshIndicator(
      onRefresh: () => refresh(value: true),
      ...
    );
    
    Login or Signup to reply.
  2. onRefresh on RefreshIndicator takes a function with the signature:

     Future<void> Function()
    

    But when you provide refresh(value: true), you are no longer providing a method of that signature but are instead calling your refresh function and provides the result of that call which ends up being Future<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.:

    return RefreshIndicator(
        onRefresh: () => refresh(value: true),
    ...
    

    We are here providing () => refresh(value: true) as anonymous function which calls refresh(value: true) and returns whatever that call might return which here is Future<void>. Therefore this provided method fits the specified signature for onRefresh.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search