The argument type 'Function' can't be assigned to 'void Function()?'
The 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.
Fix it fast
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 analyze2Match what you see, then jump to its fix
- 1
The argument type 'Function' can't be assignedYour own widget stores the callback as plain FunctionFix - 2
The argument type 'void Function(int)' can't be assigned to the parameter type 'void Function()?'The callback you pass takes parametersFix - 3
The argument type 'Future<void>' can't be assigned to the parameter type 'void Function()?'You called the function instead of passing itFix
- 1
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
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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date format
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space.
2 causes, each with a fix