0

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");
        }
      }
    });
Ranjit Shrestha
  • 622
  • 6
  • 24

1 Answers1

1

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

faithomotoso
  • 385
  • 2
  • 10
  • After shifting the listener to initState() I am getting `NoSuchMethodError: The method 'dependOnInheritedWidgetOfExactType was called on null error occured.` – Ranjit Shrestha Feb 24 '21 at 08:09
  • Try stopping the build and building afresh. If that doesn't help errors usually have links to the line of code causing it, might give a better clue – faithomotoso Feb 24 '21 at 09:17