Thorpentin
Thorpentin

Reputation: 89

Dart `void` vs `Function` vs `void Function()`

Flutter's InkWell widget has an onTap argument of type void Function(). If I declare and pass a void method, I get a type error This expression has a type of 'void' so its value can't be used. If then I use a Function instead, I have to also return null; explicitly. I would like to only use a void, and leave out the explicit return statement.

Can someone explain the difference between these types?

(I was unable to find an exact answer to this, although it might sound similar to other questions.)

Upvotes: 0

Views: 2787

Answers (1)

pedro pimont
pedro pimont

Reputation: 3074

The thing is: InkWell Widget's onTap takes a callback void Function (a function that doesn't return anything) which will be run every time user presses the button.

It's hard to know for sure since you didn't share the code but I would guess that you're likely running the function instead of passing it without the parenthesis. Because you're invoking or running the function you are not passing it, instead you're passing the returned void.

Try passing the function without parenthesis so it doesn't get run.

Upvotes: 1

Related Questions