Stop listening to streams in dispose()

This commit is contained in:
Elias Projahn 2019-12-03 12:23:41 +01:00
parent 437e30933a
commit 03fb8b2c49
2 changed files with 30 additions and 2 deletions

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'backend.dart'; import 'backend.dart';
@ -14,6 +16,7 @@ class _AppState extends State<App> with SingleTickerProviderStateMixin {
AnimationController playerBarAnimation; AnimationController playerBarAnimation;
Backend backend; Backend backend;
StreamSubscription<bool> playerActiveSubscription;
@override @override
void initState() { void initState() {
@ -30,7 +33,12 @@ class _AppState extends State<App> with SingleTickerProviderStateMixin {
super.didChangeDependencies(); super.didChangeDependencies();
backend = Backend.of(context); backend = Backend.of(context);
backend.playerActive.listen((active) =>
if (playerActiveSubscription != null) {
playerActiveSubscription.cancel();
}
playerActiveSubscription = backend.playerActive.listen((active) =>
active ? playerBarAnimation.forward() : playerBarAnimation.reverse()); active ? playerBarAnimation.forward() : playerBarAnimation.reverse());
} }
@ -79,4 +87,10 @@ class _AppState extends State<App> with SingleTickerProviderStateMixin {
), ),
); );
} }
@override
void dispose() {
super.dispose();
playerActiveSubscription.cancel();
}
} }

View file

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../backend.dart'; import '../backend.dart';
@ -11,6 +13,7 @@ class _PlayPauseButtonState extends State<PlayPauseButton>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
AnimationController playPauseAnimation; AnimationController playPauseAnimation;
Backend backend; Backend backend;
StreamSubscription<bool> playingSubscription;
@override @override
void initState() { void initState() {
@ -25,7 +28,12 @@ class _PlayPauseButtonState extends State<PlayPauseButton>
super.didChangeDependencies(); super.didChangeDependencies();
backend = Backend.of(context); backend = Backend.of(context);
backend.playing.listen((playing) =>
if (playingSubscription != null) {
playingSubscription.cancel();
}
playingSubscription = backend.playing.listen((playing) =>
playing ? playPauseAnimation.forward() : playPauseAnimation.reverse()); playing ? playPauseAnimation.forward() : playPauseAnimation.reverse());
} }
@ -39,4 +47,10 @@ class _PlayPauseButtonState extends State<PlayPauseButton>
onPressed: backend.playPause, onPressed: backend.playPause,
); );
} }
@override
void dispose() {
super.dispose();
playingSubscription.cancel();
}
} }