From d82e8807d129d2a482d5dd83a67c196716e40404 Mon Sep 17 00:00:00 2001 From: Joey Boerwinkel Date: Wed, 13 Apr 2022 11:43:56 +0200 Subject: [PATCH] cancel timer once disposed --- example/ios/Runner/Info.plist | 4 ++++ example/pubspec.lock | 10 +++++----- lib/src/google_map.dart | 16 ++++++++++------ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/example/ios/Runner/Info.plist b/example/ios/Runner/Info.plist index b5a66b3..10de3d3 100644 --- a/example/ios/Runner/Info.plist +++ b/example/ios/Runner/Info.plist @@ -41,5 +41,9 @@ UIViewControllerBasedStatusBarAppearance + NSCameraUsageDescription + This app requires access to the camera. + NSPhotoLibraryUsageDescription + This app requires access to the photo library. diff --git a/example/pubspec.lock b/example/pubspec.lock index 7333704..bedfe3c 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -7,7 +7,7 @@ packages: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.8.1" + version: "2.8.2" boolean_selector: dependency: transitive description: @@ -21,7 +21,7 @@ packages: name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0" charcode: dependency: transitive description: @@ -183,7 +183,7 @@ packages: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.10" + version: "0.12.11" meta: dependency: transitive description: @@ -272,7 +272,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.2" + version: "0.4.3" typed_data: dependency: transitive description: @@ -286,7 +286,7 @@ packages: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.0" + version: "2.1.1" sdks: dart: ">=2.14.0 <3.0.0" flutter: ">=2.5.0" diff --git a/lib/src/google_map.dart b/lib/src/google_map.dart index 5021289..2a5b00a 100644 --- a/lib/src/google_map.dart +++ b/lib/src/google_map.dart @@ -87,8 +87,8 @@ class _GoogleTrackTraceMapState extends State { DateTime lastRouteUpdate = DateTime.now(); - late final Timer routeCalculateTimer; - late final Timer markerUpdateTimer; + Timer? routeCalculateTimer; + Timer? markerUpdateTimer; @override void initState() { @@ -105,9 +105,9 @@ class _GoogleTrackTraceMapState extends State { @override void dispose() { + routeCalculateTimer?.cancel(); + markerUpdateTimer?.cancel(); controller.dispose(); - routeCalculateTimer.cancel(); - markerUpdateTimer.cancel(); super.dispose(); } @@ -206,6 +206,10 @@ class _GoogleTrackTraceMapState extends State { (widget.timerPrecision == TimePrecision.everyMinute) ? 60 : 1; markerUpdateTimer = Timer.periodic(Duration(seconds: updateInterval), (timer) { + if (mounted) { + timer.cancel(); + return; + } if (controller.route != null) { checkDestinationCloseBy(); controller.route = TrackTraceRoute( @@ -252,8 +256,8 @@ class _GoogleTrackTraceMapState extends State { controller.end.position, ) < widget.markerUpdatePrecision) { - routeCalculateTimer.cancel(); - markerUpdateTimer.cancel(); + routeCalculateTimer?.cancel(); + markerUpdateTimer?.cancel(); if (controller.route != null) { controller.route!.line = [controller.route!.line[1]]; controller.route!.distance = 0;