@@ -323,12 +323,7 @@ class CameraController extends ValueNotifier<CameraValue> {
323
323
///
324
324
/// Throws a [CameraException] if the capture fails.
325
325
Future <XFile > takePicture () async {
326
- if (! value.isInitialized || _isDisposed) {
327
- throw CameraException (
328
- 'Uninitialized CameraController.' ,
329
- 'takePicture was called on uninitialized CameraController' ,
330
- );
331
- }
326
+ _throwIfNotInitialized ("takePicture" );
332
327
if (value.isTakingPicture) {
333
328
throw CameraException (
334
329
'Previous capture has not returned yet.' ,
@@ -366,13 +361,7 @@ class CameraController extends ValueNotifier<CameraValue> {
366
361
Future <void > startImageStream (onLatestImageAvailable onAvailable) async {
367
362
assert (defaultTargetPlatform == TargetPlatform .android ||
368
363
defaultTargetPlatform == TargetPlatform .iOS);
369
-
370
- if (! value.isInitialized || _isDisposed) {
371
- throw CameraException (
372
- 'Uninitialized CameraController' ,
373
- 'startImageStream was called on uninitialized CameraController.' ,
374
- );
375
- }
364
+ _throwIfNotInitialized ("startImageStream" );
376
365
if (value.isRecordingVideo) {
377
366
throw CameraException (
378
367
'A video recording is already started.' ,
@@ -412,13 +401,7 @@ class CameraController extends ValueNotifier<CameraValue> {
412
401
Future <void > stopImageStream () async {
413
402
assert (defaultTargetPlatform == TargetPlatform .android ||
414
403
defaultTargetPlatform == TargetPlatform .iOS);
415
-
416
- if (! value.isInitialized || _isDisposed) {
417
- throw CameraException (
418
- 'Uninitialized CameraController' ,
419
- 'stopImageStream was called on uninitialized CameraController.' ,
420
- );
421
- }
404
+ _throwIfNotInitialized ("stopImageStream" );
422
405
if (value.isRecordingVideo) {
423
406
throw CameraException (
424
407
'A video recording is already started.' ,
@@ -448,12 +431,7 @@ class CameraController extends ValueNotifier<CameraValue> {
448
431
/// The video is returned as a [XFile] after calling [stopVideoRecording] .
449
432
/// Throws a [CameraException] if the capture fails.
450
433
Future <void > startVideoRecording () async {
451
- if (! value.isInitialized || _isDisposed) {
452
- throw CameraException (
453
- 'Uninitialized CameraController' ,
454
- 'startVideoRecording was called on uninitialized CameraController' ,
455
- );
456
- }
434
+ _throwIfNotInitialized ("startVideoRecording" );
457
435
if (value.isRecordingVideo) {
458
436
throw CameraException (
459
437
'A video recording is already started.' ,
@@ -483,12 +461,7 @@ class CameraController extends ValueNotifier<CameraValue> {
483
461
///
484
462
/// Throws a [CameraException] if the capture failed.
485
463
Future <XFile > stopVideoRecording () async {
486
- if (! value.isInitialized || _isDisposed) {
487
- throw CameraException (
488
- 'Uninitialized CameraController' ,
489
- 'stopVideoRecording was called on uninitialized CameraController' ,
490
- );
491
- }
464
+ _throwIfNotInitialized ("stopVideoRecording" );
492
465
if (! value.isRecordingVideo) {
493
466
throw CameraException (
494
467
'No video is recording' ,
@@ -511,12 +484,7 @@ class CameraController extends ValueNotifier<CameraValue> {
511
484
///
512
485
/// This feature is only available on iOS and Android sdk 24+.
513
486
Future <void > pauseVideoRecording () async {
514
- if (! value.isInitialized || _isDisposed) {
515
- throw CameraException (
516
- 'Uninitialized CameraController' ,
517
- 'pauseVideoRecording was called on uninitialized CameraController' ,
518
- );
519
- }
487
+ _throwIfNotInitialized ("pauseVideoRecording" );
520
488
if (! value.isRecordingVideo) {
521
489
throw CameraException (
522
490
'No video is recording' ,
@@ -535,12 +503,7 @@ class CameraController extends ValueNotifier<CameraValue> {
535
503
///
536
504
/// This feature is only available on iOS and Android sdk 24+.
537
505
Future <void > resumeVideoRecording () async {
538
- if (! value.isInitialized || _isDisposed) {
539
- throw CameraException (
540
- 'Uninitialized CameraController' ,
541
- 'resumeVideoRecording was called on uninitialized CameraController' ,
542
- );
543
- }
506
+ _throwIfNotInitialized ("resumeVideoRecording" );
544
507
if (! value.isRecordingVideo) {
545
508
throw CameraException (
546
509
'No video is recording' ,
@@ -557,12 +520,7 @@ class CameraController extends ValueNotifier<CameraValue> {
557
520
558
521
/// Returns a widget showing a live camera preview.
559
522
Widget buildPreview () {
560
- if (! value.isInitialized || _isDisposed) {
561
- throw CameraException (
562
- 'Uninitialized CameraController' ,
563
- 'buildView() was called on uninitialized CameraController.' ,
564
- );
565
- }
523
+ _throwIfNotInitialized ("buildPreview" );
566
524
try {
567
525
return CameraPlatform .instance.buildPreview (_cameraId);
568
526
} on PlatformException catch (e) {
@@ -572,13 +530,7 @@ class CameraController extends ValueNotifier<CameraValue> {
572
530
573
531
/// Gets the maximum supported zoom level for the selected camera.
574
532
Future <double > getMaxZoomLevel () {
575
- if (! value.isInitialized || _isDisposed) {
576
- throw CameraException (
577
- 'Uninitialized CameraController' ,
578
- 'getMaxZoomLevel was called on uninitialized CameraController' ,
579
- );
580
- }
581
-
533
+ _throwIfNotInitialized ("getMaxZoomLevel" );
582
534
try {
583
535
return CameraPlatform .instance.getMaxZoomLevel (_cameraId);
584
536
} on PlatformException catch (e) {
@@ -588,13 +540,7 @@ class CameraController extends ValueNotifier<CameraValue> {
588
540
589
541
/// Gets the minimum supported zoom level for the selected camera.
590
542
Future <double > getMinZoomLevel () {
591
- if (! value.isInitialized || _isDisposed) {
592
- throw CameraException (
593
- 'Uninitialized CameraController' ,
594
- 'getMinZoomLevel was called on uninitialized CameraController' ,
595
- );
596
- }
597
-
543
+ _throwIfNotInitialized ("getMinZoomLevel" );
598
544
try {
599
545
return CameraPlatform .instance.getMinZoomLevel (_cameraId);
600
546
} on PlatformException catch (e) {
@@ -608,13 +554,7 @@ class CameraController extends ValueNotifier<CameraValue> {
608
554
/// zoom level returned by the `getMaxZoomLevel` . Throws an `CameraException`
609
555
/// when an illegal zoom level is suplied.
610
556
Future <void > setZoomLevel (double zoom) {
611
- if (! value.isInitialized || _isDisposed) {
612
- throw CameraException (
613
- 'Uninitialized CameraController' ,
614
- 'setZoomLevel was called on uninitialized CameraController' ,
615
- );
616
- }
617
-
557
+ _throwIfNotInitialized ("setZoomLevel" );
618
558
try {
619
559
return CameraPlatform .instance.setZoomLevel (_cameraId, zoom);
620
560
} on PlatformException catch (e) {
@@ -666,13 +606,7 @@ class CameraController extends ValueNotifier<CameraValue> {
666
606
667
607
/// Gets the minimum supported exposure offset for the selected camera in EV units.
668
608
Future <double > getMinExposureOffset () async {
669
- if (! value.isInitialized || _isDisposed) {
670
- throw CameraException (
671
- 'Uninitialized CameraController' ,
672
- 'getMinExposureOffset was called on uninitialized CameraController' ,
673
- );
674
- }
675
-
609
+ _throwIfNotInitialized ("getMinExposureOffset" );
676
610
try {
677
611
return CameraPlatform .instance.getMinExposureOffset (_cameraId);
678
612
} on PlatformException catch (e) {
@@ -682,13 +616,7 @@ class CameraController extends ValueNotifier<CameraValue> {
682
616
683
617
/// Gets the maximum supported exposure offset for the selected camera in EV units.
684
618
Future <double > getMaxExposureOffset () async {
685
- if (! value.isInitialized || _isDisposed) {
686
- throw CameraException (
687
- 'Uninitialized CameraController' ,
688
- 'getMaxExposureOffset was called on uninitialized CameraController' ,
689
- );
690
- }
691
-
619
+ _throwIfNotInitialized ("getMaxExposureOffset" );
692
620
try {
693
621
return CameraPlatform .instance.getMaxExposureOffset (_cameraId);
694
622
} on PlatformException catch (e) {
@@ -700,13 +628,7 @@ class CameraController extends ValueNotifier<CameraValue> {
700
628
///
701
629
/// Returns 0 when the camera supports using a free value without stepping.
702
630
Future <double > getExposureOffsetStepSize () async {
703
- if (! value.isInitialized || _isDisposed) {
704
- throw CameraException (
705
- 'Uninitialized CameraController' ,
706
- 'getExposureOffsetStepSize was called on uninitialized CameraController' ,
707
- );
708
- }
709
-
631
+ _throwIfNotInitialized ("getExposureOffsetStepSize" );
710
632
try {
711
633
return CameraPlatform .instance.getExposureOffsetStepSize (_cameraId);
712
634
} on PlatformException catch (e) {
@@ -726,13 +648,7 @@ class CameraController extends ValueNotifier<CameraValue> {
726
648
///
727
649
/// Returns the (rounded) offset value that was set.
728
650
Future <double > setExposureOffset (double offset) async {
729
- if (! value.isInitialized || _isDisposed) {
730
- throw CameraException (
731
- 'Uninitialized CameraController' ,
732
- 'setExposureOffset was called on uninitialized CameraController' ,
733
- );
734
- }
735
-
651
+ _throwIfNotInitialized ("setExposureOffset" );
736
652
// Check if offset is in range
737
653
List <double > range =
738
654
await Future .wait ([getMinExposureOffset (), getMaxExposureOffset ()]);
@@ -834,4 +750,19 @@ class CameraController extends ValueNotifier<CameraValue> {
834
750
await CameraPlatform .instance.dispose (_cameraId);
835
751
}
836
752
}
753
+
754
+ void _throwIfNotInitialized (String functionName) {
755
+ if (! value.isInitialized) {
756
+ throw CameraException (
757
+ 'Uninitialized CameraController' ,
758
+ '$functionName () was called on an uninitialized CameraController.' ,
759
+ );
760
+ }
761
+ if (_isDisposed) {
762
+ throw CameraException (
763
+ 'Disposed CameraController' ,
764
+ '$functionName () was called on a disposed CameraController.' ,
765
+ );
766
+ }
767
+ }
837
768
}
0 commit comments