From c25ee046580e9944230e2a19bda6df606ec657ba Mon Sep 17 00:00:00 2001 From: Vishnu Mohandas Date: Tue, 5 May 2020 18:26:24 +0530 Subject: [PATCH] Cancel subscriptions on dispose --- lib/ui/gallery.dart | 9 ++++++++- lib/ui/gallery_app_bar_widget.dart | 10 +++++++++- lib/ui/home_widget.dart | 8 +++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/ui/gallery.dart b/lib/ui/gallery.dart index 968203069..466266a86 100644 --- a/lib/ui/gallery.dart +++ b/lib/ui/gallery.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:collection'; import 'package:flutter/cupertino.dart'; @@ -29,10 +30,11 @@ class _GalleryState extends State { final List> _collatedPhotos = List>(); Set _selectedPhotos = HashSet(); List _photos; + StreamSubscription _subscription; @override void initState() { - Bus.instance.on().listen((event) { + _subscription = Bus.instance.on().listen((event) { setState(() {}); }); super.initState(); @@ -179,4 +181,9 @@ class _GalleryState extends State { firstDate.month == secondDate.month && firstDate.day == secondDate.day; } + + void dispose() { + _subscription.cancel(); + super.dispose(); + } } diff --git a/lib/ui/gallery_app_bar_widget.dart b/lib/ui/gallery_app_bar_widget.dart index 7e2da660c..915707537 100644 --- a/lib/ui/gallery_app_bar_widget.dart +++ b/lib/ui/gallery_app_bar_widget.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:photos/core/event_bus.dart'; @@ -28,10 +30,11 @@ class GalleryAppBarWidget extends StatefulWidget class _GalleryAppBarWidgetState extends State { bool _hasSyncErrors = false; + StreamSubscription _subscription; @override void initState() { - Bus.instance.on().listen((event) { + _subscription = Bus.instance.on().listen((event) { setState(() { _hasSyncErrors = !event.success; }); @@ -162,4 +165,9 @@ class _GalleryAppBarWidgetState extends State { ), ); } + + void dispose() { + _subscription.cancel(); + super.dispose(); + } } diff --git a/lib/ui/home_widget.dart b/lib/ui/home_widget.dart index 3fdaa0342..f65be0a4e 100644 --- a/lib/ui/home_widget.dart +++ b/lib/ui/home_widget.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:collection'; import 'package:flutter/cupertino.dart'; @@ -28,13 +29,14 @@ class _HomeWidgetState extends State { ShakeDetector _detector; int _selectedNavBarItem = 0; Set _selectedPhotos = HashSet(); + StreamSubscription _subscription; @override void initState() { - Bus.instance.on().listen((event) { + _subscription = Bus.instance.on().listen((event) { setState(() {}); }); - _detector = ShakeDetector.waitForStart( + _detector = ShakeDetector.autoStart( shakeThresholdGravity: 3, onPhoneShake: () { _logger.info("Emailing logs"); @@ -45,7 +47,6 @@ class _HomeWidgetState extends State { @override Widget build(BuildContext context) { - _detector.startListening(); return Scaffold( appBar: GalleryAppBarWidget( widget.title, @@ -104,6 +105,7 @@ class _HomeWidgetState extends State { @override void dispose() { _detector.stopListening(); + _subscription.cancel(); super.dispose(); } }