Ranjit Shrestha
Ranjit Shrestha

Reputation: 762

Flutter How to Pop out page automatically according to stream event?

I want to pop out a music player page when the audio stream is stopped. But I am getting error like

Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe

This is my code which I have placed inside the build method before returning scaffold.

AudioService.playbackStateStream.listen((event) {
      if (event?.processingState == AudioProcessingState.stopped) {
        
        try {
          final modal = ModalRoute.of(context);
          if (modal.isCurrent) {
            Navigator.of(context).pop();
          }
        } catch (e) {
          print("exception error in player adv $e");
        }
      }
    });

Upvotes: 0

Views: 651

Answers (1)

faithomotoso
faithomotoso

Reputation: 454

Try moving that to initState() it's possible that everytime the widget rebuilds, the listener is added, therefore you end up having multiple listeners and when triggered would call pop multiple times

Upvotes: 1

Related Questions