flutterfunctiondartflutter-onpressed

Flutter/Dart - Why would onPressed on my iconButton work


I have the following IconButton in Flutter which when onPressed, triggers an audioplayer to play;

  IconButton(
          key: const Key('play_button'),
          onPressed: _play,
          iconSize: 30,
          icon: Icon(PlayerButtons.playbuttonbig),
        ),

But why would it fail to play when I do this instead?

  IconButton(
          key: const Key('play_button'),
          onPressed:(){
            _play;
          },
          iconSize: 30,
          icon: Icon(PlayerButtons.playbuttonbig),
        ),

The reason I need to use the 2nd is because I want to call another function before or after "_play"


Solution

  • IconButton(
              key: const Key('play_button'),
              onPressed:(){
                _play(); //<- call the play function like this
              }, 
              iconSize: 30,
              icon: Icon(PlayerButtons.playbuttonbig),
            ),