Code will explain all:
class ResultOverlay extends StatefulWidget { final bool _isCorrect; VoidCallback _onTap; ResultOverlay(this._isCorrect, this._onTap); ...... ...... }
Its state class:
class ResultOverlayState extends State<ResultOverlay>{ @override Widget build(BuildContext context) { ........ child: new InkWell( onTap: () => widget._onTap, ..... ..... }
Passing of callback function:
new ResultOverlay(isCorrect, () { () => CallAnswerPage(); })
What I am missing here?
Dart allows callback functions but also provides the Future and Completer pair of types that work together to provide a future value that can be passed around your app. Only when the async call completes does the future value get a real value.
This is a more general answer for future viewers.
There are a few different types of predefined callbacks:
final VoidCallback myVoidCallback = () {}; final ValueGetter<int> myValueGetter = () => 42; final ValueSetter<int> myValueSetter = (value) {};
Notes:
See this answer for more details.
When you are asked to provide a callback to an API, you can directly write the callback:
onPressed: () {},
Or you can supply the callback variable name (without parentheses):
onPressed: myVoidCallback,
It would be unnecessarily verbose to use both forms (but you could if you included the parentheses after the variable name):
onPressed: () { myVoidCallback(); },
This one is equivalent (but also unnecessarily verbose):
onPressed: () => myVoidCallback(),
Just use one of the "Good" forms from above.
The exception would be if you wanted to do something like call a value setter when the parameter is only asking for a void callback:
onPressed: () => myValueSetter(42),
I was not passing the callback correctly. Here is the correct syntax:
new ResultOverlay( isCorrect, () => callAnswerPage() )
So silly mistake :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With