From 4cd223a7e5d5d26a30b3a9afd9021ef44ade39a0 Mon Sep 17 00:00:00 2001 From: Collin Jackson Date: Wed, 26 Aug 2015 10:45:05 -0700 Subject: [PATCH] Cancel timer when un-mounting date picker widget --- packages/flutter/lib/widgets/date_picker.dart | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/widgets/date_picker.dart b/packages/flutter/lib/widgets/date_picker.dart index a24243e756..3197660d66 100644 --- a/packages/flutter/lib/widgets/date_picker.dart +++ b/packages/flutter/lib/widgets/date_picker.dart @@ -299,12 +299,16 @@ class MonthPicker extends ScrollableWidgetList { } DateTime _currentDate; + Timer _timer; + void _updateCurrentDate() { _currentDate = new DateTime.now(); DateTime tomorrow = new DateTime(_currentDate.year, _currentDate.month, _currentDate.day + 1); Duration timeUntilTomorrow = tomorrow.difference(_currentDate); timeUntilTomorrow += const Duration(seconds: 1); // so we don't miss it by rounding - new Timer(timeUntilTomorrow, () { + if (_timer != null) + _timer.cancel(); + _timer = new Timer(timeUntilTomorrow, () { setState(() { _updateCurrentDate(); }); @@ -332,6 +336,13 @@ class MonthPicker extends ScrollableWidgetList { } return result; } + + void didUnmount() { + super.didUnmount(); + if (_timer != null) { + _timer.cancel(); + } + } } // Scrollable list of years to allow picking a year