Home > Net >  The argument type 'Function' can't be assigned to the parameter type 'void Funct
The argument type 'Function' can't be assigned to the parameter type 'void Funct

Time:10-12

enter image description here

I'm not able to pass function to the Gesture Detector on Tap? But I've seen it worked in previous vesion of dart. Can anyone have a answer to this?

CodePudding user response:

You get the error because you defined a function without a return value. That means your function could also return an int, boolean, etc., but the onTap wants to return a void. You need to create a typedef and define the function for this to work.

Create a typedef

typedef OnPressCallback = void Function();

Declare your function as OnPressCallback type

OnPressCallback onPress;

Pass it to the onTap

onTap: onPress,

Or if you dont wan't to use a typedef you can call it like this:

onTap: (){
      onPress.call();
});

Btw there are predefined typedefs from the Flutter Framework, so you don't have to define them again:

  • GestureTapCallback

  • VoidCallback

CodePudding user response:

There are two easy options:

One:

final Function() onPress

Two:

onTap: () => onPress

CodePudding user response:

Juan Pena told you all you need to do

  • Related