Lifecycle of a custom action

Does anyone know whether a custom action is a singleton or static function? It appears to be. By that, I mean if I have one button that invokes a custom action and that action stores a value outside the function, will that value be available the next time I invoke the same custom action from another button? It appears to work so I'm hoping that is reliably the case. The following code should show what I mean. Player is declared outside the function and seems to retain its value as launchPlayer is first called with a play parameter and later called with a stop parameter.

AudioPlayer? player = null; // Initialise as not created

Future<int> launchPlayer(
  String command,
) async {
  int returnStatus = 0;

  if (player == null) {
    // First time entry so create audio player.  Next entry, use same one.
    player = AudioPlayer();
  }

  switch (command) {
    case 'play':
      duration = await player?.setUrl("whatever.mp3");
      player?.play();
    case 'stop':
      player?.stop();
    default:
      returnStatus = -1;
  }

  return returnStatus;
}
2
2 replies