@@ -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) {
0 commit comments