Skip to content

Commit

Permalink
Returns unsupported errors on web (#1440)
Browse files Browse the repository at this point in the history
* Returns unsupported errors on web

* Changed PlatformException for Unsupported error

* Fix analysis

* Fixed tests

* removed unused import
  • Loading branch information
TimHoogstrate committed Feb 15, 2024
1 parent bc2142e commit a57e19b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion geolocator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ The second key (in this example called `YourPurposeKey`) should match the purpos

To use the Geolocator plugin on the web you need to be using Flutter 1.20 or higher. Flutter will automatically add the endorsed [geolocator_web]() package to your application when you add the `geolocator: ^6.2.0` dependency to your `pubspec.yaml`.

The following methods of the geolocator API are not supported on the web and will result in a `PlatformException` with the code `UNSUPPORTED_OPERATION`:
The following methods of the geolocator API are not supported on the web and will result in a `UnsupportedError`:

- `getLastKnownPosition({ bool forceAndroidLocationManager = true })`
- `openAppSettings()`
- `openLocationSettings()`
- `getServiceStatusStream()`

**NOTE**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ abstract class GeolocatorPlatform extends PlatformInterface {
///
/// An instance of [LocationServiceStatus] will be emitted each time the
/// location service is enabled or disabled.
/// Throws an [UnimplementedError] on the web as the concept of location
/// service doesn't exist on the web platform.
Stream<ServiceStatus> getServiceStatusStream() {
throw UnimplementedError(
'getServiceStatusStream() has not been implemented.');
Expand Down
5 changes: 5 additions & 0 deletions geolocator_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0

**BREAKING CHANGE:**
- `getServiceStatusStream` on web returns a PlatformException i.s.o. UnimplementedError. As the concept of location service doesn't exist on the web platform.

## 2.2.1

- Migrates tests to support sound null safety.
Expand Down
12 changes: 7 additions & 5 deletions geolocator_web/lib/geolocator_web.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:geolocator_platform_interface/geolocator_platform_interface.dart';
import 'src/geolocation_manager.dart';
Expand Down Expand Up @@ -115,6 +114,10 @@ class GeolocatorPlugin extends GeolocatorPlatform {
});
}

@override
Stream<ServiceStatus> getServiceStatusStream() =>
throw _unsupported('getServiceStatusStream');

@override
Future<bool> openAppSettings() => throw _unsupported('openAppSettings');

Expand All @@ -139,10 +142,9 @@ class GeolocatorPlugin extends GeolocatorPlatform {
}
}

PlatformException _unsupported(String method) {
return PlatformException(
code: 'UNSUPPORTED_OPERATION',
message: '$method is not supported on the web platform.',
UnsupportedError _unsupported(String method) {
return UnsupportedError(
'$method is not supported on the web platform.',
);
}
}
2 changes: 1 addition & 1 deletion geolocator_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: geolocator_web
description: Official web implementation of the geolocator plugin.
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_web
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
version: 2.2.1
version: 3.0.0

flutter:
plugin:
Expand Down
12 changes: 8 additions & 4 deletions geolocator_web/test/geolocator_web_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:geolocator_web/geolocator_web.dart';
import 'package:geolocator_web/src/geolocation_manager.dart';
Expand Down Expand Up @@ -117,17 +116,22 @@ void main() {
group('Unsupported exceptions', () {
test('getLastKnownPosition throws unsupported exception', () async {
expect(geolocatorPlugin.getLastKnownPosition,
throwsA(isA<PlatformException>()));
throwsA(isA<UnsupportedError>()));
});

test('openAppSettings throws unsupported exception', () async {
expect(
geolocatorPlugin.openAppSettings, throwsA(isA<PlatformException>()));
geolocatorPlugin.openAppSettings, throwsA(isA<UnsupportedError>()));
});

test('openLocationSettings throws unsupported exception', () async {
expect(geolocatorPlugin.openLocationSettings,
throwsA(isA<PlatformException>()));
throwsA(isA<UnsupportedError>()));
});

test('getServiceStatusStream throws unsupported exception', () async {
expect(geolocatorPlugin.getServiceStatusStream,
throwsA(isA<UnsupportedError>()));
});
});
}

0 comments on commit a57e19b

Please sign in to comment.