skip to Main Content

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


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

    Login or Signup to reply.
  2. onPressed: auidoHandler.play,
    

    is equivalent to

    onPressed: () {
      auidoHandler.play();
    },
    

    or

    onPressed: () => auidoHandler.play(),
    

    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

    onPressed : () -> {auidoHandler.play}
    

    with

    onPressed: () {
      auidoHandler.play();
    },
    

    or

    onPressed: () => auidoHandler.play(),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search