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