diff --git a/geolocator_android/lib/src/geolocator_android.dart b/geolocator_android/lib/src/geolocator_android.dart index 04c1ced86..ec94d0731 100644 --- a/geolocator_android/lib/src/geolocator_android.dart +++ b/geolocator_android/lib/src/geolocator_android.dart @@ -1,5 +1,6 @@ import 'dart:async'; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:geolocator_android/geolocator_android.dart'; import 'package:geolocator_platform_interface/geolocator_platform_interface.dart'; @@ -8,18 +9,15 @@ import 'package:uuid/uuid.dart'; /// An implementation of [GeolocatorPlatform] that uses method channels. class GeolocatorAndroid extends GeolocatorPlatform { /// The method channel used to interact with the native platform. - static const _methodChannel = - MethodChannel('flutter.baseflow.com/geolocator_android'); + static const _methodChannel = MethodChannel('flutter.baseflow.com/geolocator_android'); /// The event channel used to receive [Position] updates from the native /// platform. - static const _eventChannel = - EventChannel('flutter.baseflow.com/geolocator_updates_android'); + static const _eventChannel = EventChannel('flutter.baseflow.com/geolocator_updates_android'); /// The event channel used to receive [LocationServiceStatus] updates from the /// native platform. - static const _serviceStatusEventChannel = - EventChannel('flutter.baseflow.com/geolocator_service_updates_android'); + static const _serviceStatusEventChannel = EventChannel('flutter.baseflow.com/geolocator_service_updates_android'); /// Registers this class as the default instance of [GeolocatorPlatform]. static void registerWith() { @@ -41,8 +39,7 @@ class GeolocatorAndroid extends GeolocatorPlatform { Future checkPermission() async { try { // ignore: omit_local_variable_types - final int permission = - await _methodChannel.invokeMethod('checkPermission'); + final int permission = await _methodChannel.invokeMethod('checkPermission'); return permission.toLocationPermission(); } on PlatformException catch (e) { @@ -56,8 +53,7 @@ class GeolocatorAndroid extends GeolocatorPlatform { Future requestPermission() async { try { // ignore: omit_local_variable_types - final int permission = - await _methodChannel.invokeMethod('requestPermission'); + final int permission = await _methodChannel.invokeMethod('requestPermission'); return permission.toLocationPermission(); } on PlatformException catch (e) { @@ -68,9 +64,8 @@ class GeolocatorAndroid extends GeolocatorPlatform { } @override - Future isLocationServiceEnabled() async => _methodChannel - .invokeMethod('isLocationServiceEnabled') - .then((value) => value ?? false); + Future isLocationServiceEnabled() async => + _methodChannel.invokeMethod('isLocationServiceEnabled').then((value) => value ?? false); @override Future getLastKnownPosition({ @@ -81,8 +76,7 @@ class GeolocatorAndroid extends GeolocatorPlatform { 'forceLocationManager': forceLocationManager, }; - final positionMap = - await _methodChannel.invokeMethod('getLastKnownPosition', parameters); + final positionMap = await _methodChannel.invokeMethod('getLastKnownPosition', parameters); return positionMap != null ? AndroidPosition.fromMap(positionMap) : null; } on PlatformException catch (e) { @@ -94,8 +88,7 @@ class GeolocatorAndroid extends GeolocatorPlatform { @override Future getLocationAccuracy() async { - final int accuracy = - await _methodChannel.invokeMethod('getLocationAccuracy'); + final int accuracy = await _methodChannel.invokeMethod('getLocationAccuracy'); return LocationAccuracyStatus.values[accuracy]; } @@ -146,12 +139,10 @@ class GeolocatorAndroid extends GeolocatorPlatform { if (_serviceStatusStream != null) { return _serviceStatusStream!; } - var serviceStatusStream = - _serviceStatusEventChannel.receiveBroadcastStream(); + var serviceStatusStream = _serviceStatusEventChannel.receiveBroadcastStream(); - _serviceStatusStream = serviceStatusStream - .map((dynamic element) => ServiceStatus.values[element as int]) - .handleError((error) { + _serviceStatusStream = + serviceStatusStream.map((dynamic element) => ServiceStatus.values[element as int]).handleError((error) { _serviceStatusStream = null; if (error is PlatformException) { error = _handlePlatformException(error); @@ -162,46 +153,51 @@ class GeolocatorAndroid extends GeolocatorPlatform { return _serviceStatusStream!; } + // Ryde Modification: Added try catch logic @override Stream getPositionStream({ LocationSettings? locationSettings, }) { - if (_positionStream != null) { - return _positionStream!; - } - var originalStream = _eventChannel.receiveBroadcastStream( - locationSettings?.toJson(), - ); - var positionStream = _wrapStream(originalStream); - - var timeLimit = locationSettings?.timeLimit; - - if (timeLimit != null) { - positionStream = positionStream.timeout( - timeLimit, - onTimeout: (s) { - _positionStream = null; - s.addError(TimeoutException( - 'Time limit reached while waiting for position update.', - timeLimit, - )); - s.close(); + try { + if (_positionStream != null) { + return _positionStream!; + } + var originalStream = _eventChannel.receiveBroadcastStream( + locationSettings?.toJson(), + ); + var positionStream = _wrapStream(originalStream); + + var timeLimit = locationSettings?.timeLimit; + + if (timeLimit != null) { + positionStream = positionStream.timeout( + timeLimit, + onTimeout: (s) { + _positionStream = null; + s.addError(TimeoutException( + 'Time limit reached while waiting for position update.', + timeLimit, + )); + s.close(); + }, + ); + } + + _positionStream = positionStream + .map((dynamic element) => AndroidPosition.fromMap(element.cast())) + .handleError( + (error) { + if (error is PlatformException) { + error = _handlePlatformException(error); + } + throw error; }, ); + return _positionStream!; + } catch (e) { + if (kDebugMode) print(e); + return const Stream.empty(); } - - _positionStream = positionStream - .map((dynamic element) => - AndroidPosition.fromMap(element.cast())) - .handleError( - (error) { - if (error is PlatformException) { - error = _handlePlatformException(error); - } - throw error; - }, - ); - return _positionStream!; } Stream _wrapStream(Stream incoming) { @@ -230,14 +226,12 @@ class GeolocatorAndroid extends GeolocatorPlatform { } @override - Future openAppSettings() async => _methodChannel - .invokeMethod('openAppSettings') - .then((value) => value ?? false); + Future openAppSettings() async => + _methodChannel.invokeMethod('openAppSettings').then((value) => value ?? false); @override - Future openLocationSettings() async => _methodChannel - .invokeMethod('openLocationSettings') - .then((value) => value ?? false); + Future openLocationSettings() async => + _methodChannel.invokeMethod('openLocationSettings').then((value) => value ?? false); Exception _handlePlatformException(PlatformException exception) { switch (exception.code) { diff --git a/geolocator_apple/lib/src/geolocator_apple.dart b/geolocator_apple/lib/src/geolocator_apple.dart index 8475bdd2d..07c395db7 100644 --- a/geolocator_apple/lib/src/geolocator_apple.dart +++ b/geolocator_apple/lib/src/geolocator_apple.dart @@ -7,18 +7,15 @@ import 'package:geolocator_platform_interface/geolocator_platform_interface.dart /// An implementation of [GeolocatorPlatform] that uses method channels. class GeolocatorApple extends GeolocatorPlatform { /// The method channel used to interact with the native platform. - static const _methodChannel = - MethodChannel('flutter.baseflow.com/geolocator_apple'); + static const _methodChannel = MethodChannel('flutter.baseflow.com/geolocator_apple'); /// The event channel used to receive [Position] updates from the native /// platform. - static const _eventChannel = - EventChannel('flutter.baseflow.com/geolocator_updates_apple'); + static const _eventChannel = EventChannel('flutter.baseflow.com/geolocator_updates_apple'); /// The event channel used to receive [LocationServiceStatus] updates from the /// native platform. - static const _serviceStatusEventChannel = - EventChannel('flutter.baseflow.com/geolocator_service_updates_apple'); + static const _serviceStatusEventChannel = EventChannel('flutter.baseflow.com/geolocator_service_updates_apple'); /// Registers this class as the default instance of [GeolocatorPlatform]. static void registerWith() { @@ -38,8 +35,7 @@ class GeolocatorApple extends GeolocatorPlatform { Future checkPermission() async { try { // ignore: omit_local_variable_types - final int permission = - await _methodChannel.invokeMethod('checkPermission'); + final int permission = await _methodChannel.invokeMethod('checkPermission'); return permission.toLocationPermission(); } on PlatformException catch (e) { @@ -53,8 +49,7 @@ class GeolocatorApple extends GeolocatorPlatform { Future requestPermission() async { try { // ignore: omit_local_variable_types - final int permission = - await _methodChannel.invokeMethod('requestPermission'); + final int permission = await _methodChannel.invokeMethod('requestPermission'); return permission.toLocationPermission(); } on PlatformException catch (e) { @@ -65,9 +60,8 @@ class GeolocatorApple extends GeolocatorPlatform { } @override - Future isLocationServiceEnabled() async => _methodChannel - .invokeMethod('isLocationServiceEnabled') - .then((value) => value ?? false); + Future isLocationServiceEnabled() async => + _methodChannel.invokeMethod('isLocationServiceEnabled').then((value) => value ?? false); @override Future getLastKnownPosition({ @@ -78,8 +72,7 @@ class GeolocatorApple extends GeolocatorPlatform { 'forceLocationManager': forceLocationManager, }; - final positionMap = - await _methodChannel.invokeMethod('getLastKnownPosition', parameters); + final positionMap = await _methodChannel.invokeMethod('getLastKnownPosition', parameters); return positionMap != null ? Position.fromMap(positionMap) : null; } on PlatformException catch (e) { @@ -91,8 +84,7 @@ class GeolocatorApple extends GeolocatorPlatform { @override Future getLocationAccuracy() async { - final int accuracy = - await _methodChannel.invokeMethod('getLocationAccuracy'); + final int accuracy = await _methodChannel.invokeMethod('getLocationAccuracy'); return LocationAccuracyStatus.values[accuracy]; } @@ -133,12 +125,10 @@ class GeolocatorApple extends GeolocatorPlatform { if (_serviceStatusStream != null) { return _serviceStatusStream!; } - var serviceStatusStream = - _serviceStatusEventChannel.receiveBroadcastStream(); + var serviceStatusStream = _serviceStatusEventChannel.receiveBroadcastStream(); - _serviceStatusStream = serviceStatusStream - .map((dynamic element) => ServiceStatus.values[element as int]) - .handleError((error) { + _serviceStatusStream = + serviceStatusStream.map((dynamic element) => ServiceStatus.values[element as int]).handleError((error) { _serviceStatusStream = null; if (error is PlatformException) { error = _handlePlatformException(error); @@ -149,46 +139,50 @@ class GeolocatorApple extends GeolocatorPlatform { return _serviceStatusStream!; } + // Ryde Modification: added try catch @override Stream getPositionStream({ LocationSettings? locationSettings, }) { - if (_positionStream != null) { - return _positionStream!; - } - var originalStream = _eventChannel.receiveBroadcastStream( - locationSettings?.toJson(), - ); - var positionStream = _wrapStream(originalStream); - - var timeLimit = locationSettings?.timeLimit; - - if (timeLimit != null) { - positionStream = positionStream.timeout( - timeLimit, - onTimeout: (s) { - _positionStream = null; - s.addError(TimeoutException( - 'Time limit reached while waiting for position update.', - timeLimit, - )); - s.close(); + try { + if (_positionStream != null) { + return _positionStream!; + } + var originalStream = _eventChannel.receiveBroadcastStream( + locationSettings?.toJson(), + ); + var positionStream = _wrapStream(originalStream); + + var timeLimit = locationSettings?.timeLimit; + + if (timeLimit != null) { + positionStream = positionStream.timeout( + timeLimit, + onTimeout: (s) { + _positionStream = null; + s.addError(TimeoutException( + 'Time limit reached while waiting for position update.', + timeLimit, + )); + s.close(); + }, + ); + } + + _positionStream = positionStream + .map((dynamic element) => Position.fromMap(element.cast())) + .handleError( + (error) { + if (error is PlatformException) { + error = _handlePlatformException(error); + } + throw error; }, ); + return _positionStream!; + } catch (e) { + return const Stream.empty(); } - - _positionStream = positionStream - .map((dynamic element) => - Position.fromMap(element.cast())) - .handleError( - (error) { - if (error is PlatformException) { - error = _handlePlatformException(error); - } - throw error; - }, - ); - return _positionStream!; } Stream _wrapStream(Stream incoming) { @@ -217,14 +211,12 @@ class GeolocatorApple extends GeolocatorPlatform { } @override - Future openAppSettings() async => _methodChannel - .invokeMethod('openAppSettings') - .then((value) => value ?? false); + Future openAppSettings() async => + _methodChannel.invokeMethod('openAppSettings').then((value) => value ?? false); @override - Future openLocationSettings() async => _methodChannel - .invokeMethod('openLocationSettings') - .then((value) => value ?? false); + Future openLocationSettings() async => + _methodChannel.invokeMethod('openLocationSettings').then((value) => value ?? false); Exception _handlePlatformException(PlatformException exception) { switch (exception.code) {