Skip to content

Issue canceling stream subscription #22

@hacker1024

Description

@hacker1024

I'm having an issue with the new stream apis, where setState is being called after my widget is disposed. I can't work out what I've done wrong.

Relevant code:

  void connect() async {
    AudioService.connect();
    playbackStateSubscription?.cancel();
    playbackStateSubscription = AudioService.playbackStateStream.listen((PlaybackState playbackState) {
      setState(() {
        _playbackState = playbackState;
        print(playbackState);
      });
    });
  }

  void stopListening() {
    playbackStateSubscription?.cancel();
    playbackStateSubscription = null;
  }

  @override
  void initState() {
    super.initState();
    connect();
  }

  @override
  void dispose() {
    stopListening();
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        connect();
        break;
      case AppLifecycleState.paused:
        stopListening();
        AudioService.disconnect();
        break;
      default:
        break;
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions