flutteraudio-service

Flutter, What is different between onPressed: () {...}. and onPressed: methodCall()


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.


Solution

  • 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.