Flutter/Dart – Why would onPressed on my iconButton work

Issue

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),
        ),

Answered By – Christian

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *