Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

The argument type 'Function' can't be assigned to 'void Function()?'

ErrorThe argument type 'Function' can't be assigned to the parameter type 'void Function()?'

Short answer

Dart no longer converts a value typed as plain Function into a specific function type for you. A widget that takes onPressed or onTap needs a value of type void Function()?. Declare your own callback as VoidCallback, match the parameter list, and pass the function without calling it.

  • Applies to Dart 3.x, Flutter 3.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the type in the first quote of the error. It is the type you pass, and it points to the cause below.

    dart analyze
  2. 2Match what you see, then jump to its fix

    1. 1The argument type 'Function' can't be assignedYour own widget stores the callback as plain FunctionFix
    2. 2The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()?'The callback you pass takes parametersFix
    3. 3The argument type 'Future<void>' can't be assigned to the parameter type 'void Function()?'You called the function instead of passing itFix

What does the error look like?

  • The argument type 'Function' can't be assigned to the parameter type 'void Function()?'
  • The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()?'
  • The argument type 'Future<void>' can't be assigned to the parameter type 'void Function()?'

Cause 1 of 3

Your own widget stores the callback as plain Function

1Check

Look for a field or constructor parameter declared as Function in your widget. Passing a Function value to a slot typed void Function()? is an implicit downcast, and Dart rejects those for non-dynamic types.

grep -rn "final Function" lib/

2Fix

Give the field a real function type. VoidCallback is the Flutter name for void Function(). Make it nullable if the callback is optional.

class SaveButton extends StatelessWidget {
  const SaveButton({super.key, this.onSave});

  // was: final Function onSave;
  final VoidCallback? onSave;

  @override
  Widget build(BuildContext context) {
    return ElevatedButton(onPressed: onSave, child: const Text('Save'));
  }
}

3Verify

The analyzer reports no error for the widget.

dart analyze lib/

Confirmed by api.flutter.dev

Did this fix it?

Cause 2 of 3

The callback you pass takes parameters

1Check

onPressed and onTap call the function with no arguments. A function that wants an int has a different type.

dart analyze lib/ | grep "can't be assigned"

2Fix

Wrap it in a closure that supplies the value, or change the slot to a type that takes the argument.

void select(int index) { /* ... */ }

TextButton(onPressed: () => select(2), child: const Text('Pick'));

3Verify

Run the analyzer again.

dart analyze lib/

Confirmed by dart.dev

Did this fix it?

Cause 3 of 3

You called the function instead of passing it

1Check

Parentheses after the name run the function and pass its result. Without parentheses you pass the function itself, which Dart calls a tear-off.

grep -rn "onPressed: [a-zA-Z]*()" lib/

2Fix

Remove the parentheses, or use a closure if you must pass arguments.

// wrong: onPressed: save(),
ElevatedButton(onPressed: save, child: const Text('Save'));

3Verify

The analyzer error disappears and the button calls save when tapped.

dart analyze lib/

Confirmed by dart.dev

Did this fix it?

How do I stop it coming back?

  • Type every callback field as VoidCallback, ValueChanged<T> or an explicit function type, never as Function.
  • To call an optional callback, write onSave?.call() instead of onSave().
  • Run dart analyze in CI so a type error never reaches a release build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.