Skip to content

Commit e4c1634

Browse files
committed
fix MissingPluginException on unsupported platforms (#149)
1 parent 3cef095 commit e4c1634

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/src/data/data_stream_factory.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../exceptions/incorrect_setup_exception.dart';
99
import '../exceptions/permission_denied_exception.dart' as lm;
1010
import '../exceptions/permission_requesting_exception.dart' as lm;
1111
import '../exceptions/service_disabled_exception.dart';
12+
import '../exceptions/unsupported_exception.dart';
1213
import '../widgets/current_location_layer.dart';
1314
import 'data.dart';
1415

@@ -63,8 +64,12 @@ class LocationMarkerDataStreamFactory {
6364
/// Create a heading stream which is used as default value of
6465
/// [CurrentLocationLayer.headingStream].
6566
Stream<OrientationEvent> defaultHeadingStreamSource() {
66-
RotationSensor.samplingPeriod = SensorInterval.uiInterval;
67-
return RotationSensor.orientationStream;
67+
if (RotationSensor.isPlatformSupported) {
68+
RotationSensor.samplingPeriod = SensorInterval.uiInterval;
69+
return RotationSensor.orientationStream;
70+
} else {
71+
return Stream.error(UnsupportedException());
72+
}
6873
}
6974
}
7075

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'package:flutter/foundation.dart';
2+
3+
/// An exception thrown when an operation is not supported on the current
4+
/// platform.
5+
class UnsupportedException implements Exception {
6+
/// Create an UnsupportedException.
7+
const UnsupportedException();
8+
9+
@override
10+
String toString() {
11+
final platform = kIsWeb ? 'web' : defaultTargetPlatform.name;
12+
return 'FlutterRotationSensor does not support the $platform platform.';
13+
}
14+
}

lib/src/widgets/current_location_layer.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '../exceptions/incorrect_setup_exception.dart';
1313
import '../exceptions/permission_denied_exception.dart';
1414
import '../exceptions/permission_requesting_exception.dart';
1515
import '../exceptions/service_disabled_exception.dart';
16+
import '../exceptions/unsupported_exception.dart';
1617
import '../options/align_on_update.dart';
1718
import '../options/focal_point.dart';
1819
import '../options/indicators.dart';
@@ -413,6 +414,10 @@ class _CurrentLocationLayerState extends State<CurrentLocationLayer>
413414
setState(() => _status = _Status.permissionDenied);
414415
case ServiceDisabledException _:
415416
setState(() => _status = _Status.serviceDisabled);
417+
case UnsupportedException unsupportedException:
418+
if (kDebugMode) {
419+
print(unsupportedException);
420+
}
416421
}
417422
_headingSubscription?.cancel();
418423
},
@@ -455,7 +460,12 @@ class _CurrentLocationLayerState extends State<CurrentLocationLayer>
455460
}
456461
}
457462
},
458-
onError: (_) {
463+
onError: (error) {
464+
if (error is UnsupportedException) {
465+
if (kDebugMode) {
466+
print(error);
467+
}
468+
}
459469
if (_animatingHeading != null) {
460470
setState(() => _animatingHeading = null);
461471
}

0 commit comments

Comments
 (0)