1

In my widget I pass a function callback as a parameter to the widget:

onTap: onTapRestaurantItemCallback

Out in the screen widget, which contains the widget above, I then execute the function callback:

onTapRestaurantItemCallback: () { // Handling the callback method }

I am using Lint for Dart/Flutter to check my code: https://pub.dev/packages/lint

The linter is not happy with my function parameter and gives me an error, I am violating the rule:

argument_type_not_assignable

"The argument type 'Function' can't be assigned to the parameter type 'void Function()'" https://dart.dev/tools/diagnostic-messages#argument_type_not_assignable

Apparently the problem is that the widgets "onTap" expects void, whereas the callback returns a Future.

I can make the error go away in the widget by writing:

onTap: () => onTapRestaurantItemCallback

But then my function callbacks stop working.

Do you know how I can pass a function callback to my widget like I do and still keep the argument_type_not_assignable lint rule?

Below I paste a more full code on the scenario I have described above:

// WIDGET USED BY OTHER WIDGET BELOW
class RestaurantItem extends StatelessWidget {
  // FUNCTION CALLBACK
  final Function onTapRestaurantItemCallback;
  const RestaurantItem({
    // FUNCTION CALLBACK
    @required this.onTapRestaurantItemCallback,
  });
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      // FUNCTION CALLBACK
      onTap: onTapRestaurantItemCallback,

// CONTAINING SCREEN WIDGET - USING WIDGET ABOVE
// .... //
RestaurantItem(
  // FUNCTION CALLBACK
  onTapRestaurantItemCallback: () => { // Handling the callback method }
),
Rune Hansen
  • 954
  • 3
  • 16
  • 36
  • 1
    `() => onTapRestaurantItemCallback` is a zero-argument function that returns a function. Since you want to *invoke* `onTapRestaurantItemCallback`, that should be `() => onTapRestaurantItemCallback()`. Is your callback `async`? Why does it return a `Future`? – jamesdlin Nov 09 '20 at 21:38
  • Great. You solved my problem. This works: onTap: () => onTapRestaurantItemCallback() How do I put you as the provider of the answer? – Rune Hansen Nov 09 '20 at 22:15

0 Answers0