I'm pretty inexperienced with Flutter, I created a widget with this piece of code inside:
With code written like this: (the ToolModify function is not called)
final VoidCallback ToolModify;
onTap: () {
// rest of the code
widget.ToolModify;
},
Instead, it is called with code written like this:
onTap: widget.ToolModify,
Can anyone explain to me why this happens?
Since I need to write other lines of code besides widget.ToolModify; how can i call the ToolModify function inside OnTap: () {...} ??
Hope someone can help me. Thanks :)
It is perfectly legal and good style to code like this:
In this case,
widget.ToolModifydenotes a function and you assign the function toonTapto makeonTapcall the function later on.If you call it like this, you make a new anonymous and parameterless function
() {}that just calls the other functionwidget.ToolModify():Note, that
widget.ToolModifydenotes the function itself. Whilewidget.ToolModify()calls the function with no parameters.Moreover, you may write this, since you return just one expression:
Example
Have a look at this example that defines a function ad(), that returns an anonymous function and later on applies it to some arguments:
Outputs this: