I am working on audio_service example.
and I seems, auidoHandler.play or pause ..else callings work only like below
onPressed: auidoHandler.play
not like
onPressed : () -> {auidoHandler.play}
I did not have time to learn about dart or flutter but i have to do.
Plese enlight me.
2
Answers
onPressed
needs to get provided a reference to a function.auidoHandler.play
is a function thus it can be used.() -> {auidoHandler.play}
is also a function but a function that does nothing because its missing the function call operator()
at the end.It should be
() -> {auidoHandler.play();}
Note:
The option stated in the title of your question
onPressed: methodCall()
would not work because the methodCall() would call the function when the component is mounted and the result whould be passed as the function to be called on the event. Unless the function returns another function this would not work.is equivalent to
or
In fact, the first version is preferred by the linter (see
unnecessary_lambdas
).Your issue is that you are not calling the method
auidoHandler.play
in your second example:Replace
with
or