Skip to content

Commit c37e276

Browse files
committed
🥅 Catch exceptions when obtain/subscribe to the accelerometer stream (#230)
(cherry picked from commit 414b7dc)
1 parent 441d1c3 commit c37e276

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

‎CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ that can be found in the LICENSE file. -->
44

55
# Changelog
66

7-
See the [Migration Guide](guides/migration_guide.md) for the details of breaking changes between versions.
7+
See the [Migration Guide](guides/migration_guide.md) for breaking changes between versions.
8+
9+
## 4.1.2
10+
11+
- Roll `sensors_plus`.
12+
- Catch exceptions when obtain/subscribe to the accelerometer stream.
813

914
## 4.1.1
1015

@@ -21,7 +26,7 @@ See the [Migration Guide](guides/migration_guide.md) for the details of breaking
2126
### Fixes
2227

2328
- Handle exceptions after all flows.
24-
- Fix various of problems with the capture button.
29+
- Fix various problems with the capture button.
2530

2631
## 4.0.3
2732

‎lib/src/states/camera_picker_state.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class CameraPickerState extends State<CameraPicker>
216216
bool retriedAfterInvalidInitialize = false;
217217

218218
/// Subscribe to the accelerometer.
219-
late final StreamSubscription<AccelerometerEvent> accelerometerSubscription;
219+
StreamSubscription<AccelerometerEvent>? accelerometerSubscription;
220220

221221
/// The locked capture orientation of the current camera instance.
222222
DeviceOrientation? lockedCaptureOrientation;
@@ -228,9 +228,7 @@ class CameraPickerState extends State<CameraPicker>
228228
Constants.textDelegate = widget.pickerConfig.textDelegate ??
229229
cameraPickerTextDelegateFromLocale(widget.locale);
230230
initCameras();
231-
accelerometerSubscription = accelerometerEvents.listen(
232-
handleAccelerometerEvent,
233-
);
231+
initAccelerometerSubscription();
234232
}
235233

236234
@override
@@ -249,7 +247,7 @@ class CameraPickerState extends State<CameraPicker>
249247
exposureFadeOutTimer?.cancel();
250248
recordDetectTimer?.cancel();
251249
recordCountdownTimer?.cancel();
252-
accelerometerSubscription.cancel();
250+
accelerometerSubscription?.cancel();
253251
super.dispose();
254252
}
255253

@@ -491,6 +489,20 @@ class CameraPickerState extends State<CameraPicker>
491489
});
492490
}
493491

492+
/// Starts to listen on accelerometer events.
493+
void initAccelerometerSubscription() {
494+
try {
495+
final stream = accelerometerEventStream();
496+
accelerometerSubscription = stream.listen(handleAccelerometerEvent);
497+
} catch (e, s) {
498+
realDebugPrint(
499+
'The device does not seem to support accelerometer. '
500+
'The captured files orientation might be incorrect.',
501+
);
502+
handleErrorWithHandler(e, s, pickerConfig.onError);
503+
}
504+
}
505+
494506
/// Lock capture orientation according to the current status of the device,
495507
/// which enables the captured file stored the correct orientation.
496508
void handleAccelerometerEvent(AccelerometerEvent event) {

‎pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: wechat_camera_picker
2-
version: 4.1.1
2+
version: 4.1.2
33
description: |
44
A camera picker for Flutter projects based on WeChat's UI,
55
which is also a separate runnable extension to the
@@ -26,7 +26,7 @@ dependencies:
2626
camera_platform_interface: ^2.1.5
2727
path: ^1.8.0
2828
photo_manager: ^2.7.0
29-
sensors_plus: ^3.1.0
29+
sensors_plus: ^4.0.1
3030
video_player: ^2.7.0
3131

3232
dev_dependencies:

0 commit comments

Comments
 (0)