Function vs Function() in Dart/Flutter

Viewed 102

What is the difference between type specifying a variable as Function vs Function().

When I declare a variable as

final Function? btnOkOnPress;

And then use/call it like this

btnOkOnPress?.call();

I get a warning/problem saying, Avoid method calls or property accesses on a "dynamic" target.

But when I declare it like

final Function()? btnOkOnPress;

There is no such warning, why is that?

Why is it calling target as dynamic even though it is defined as Function.

1 Answers

The warnings are because when you declare it this way final Function? btnOkOnPress; the details of the parameters to the function is not given.

And when you declare in the other way, it is being specified that there is no parameters for this function. You could also use final VoidCallback? btnOkOnPress;.

Related