@@ -323,12 +323,7 @@ class CameraController extends ValueNotifier<CameraValue> {
323323 ///
324324 /// Throws a [CameraException] if the capture fails.
325325 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" );
332327 if (value.isTakingPicture) {
333328 throw CameraException (
334329 'Previous capture has not returned yet.' ,
@@ -366,13 +361,7 @@ class CameraController extends ValueNotifier<CameraValue> {
366361 Future <void > startImageStream (onLatestImageAvailable onAvailable) async {
367362 assert (defaultTargetPlatform == TargetPlatform .android ||
368363 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" );
376365 if (value.isRecordingVideo) {
377366 throw CameraException (
378367 'A video recording is already started.' ,
@@ -412,13 +401,7 @@ class CameraController extends ValueNotifier<CameraValue> {
412401 Future <void > stopImageStream () async {
413402 assert (defaultTargetPlatform == TargetPlatform .android ||
414403 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" );
422405 if (value.isRecordingVideo) {
423406 throw CameraException (
424407 'A video recording is already started.' ,
@@ -448,12 +431,7 @@ class CameraController extends ValueNotifier<CameraValue> {
448431 /// The video is returned as a [XFile] after calling [stopVideoRecording] .
449432 /// Throws a [CameraException] if the capture fails.
450433 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" );
457435 if (value.isRecordingVideo) {
458436 throw CameraException (
459437 'A video recording is already started.' ,
@@ -483,12 +461,7 @@ class CameraController extends ValueNotifier<CameraValue> {
483461 ///
484462 /// Throws a [CameraException] if the capture failed.
485463 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" );
492465 if (! value.isRecordingVideo) {
493466 throw CameraException (
494467 'No video is recording' ,
@@ -511,12 +484,7 @@ class CameraController extends ValueNotifier<CameraValue> {
511484 ///
512485 /// This feature is only available on iOS and Android sdk 24+.
513486 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" );
520488 if (! value.isRecordingVideo) {
521489 throw CameraException (
522490 'No video is recording' ,
@@ -535,12 +503,7 @@ class CameraController extends ValueNotifier<CameraValue> {
535503 ///
536504 /// This feature is only available on iOS and Android sdk 24+.
537505 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" );
544507 if (! value.isRecordingVideo) {
545508 throw CameraException (
546509 'No video is recording' ,
@@ -557,12 +520,7 @@ class CameraController extends ValueNotifier<CameraValue> {
557520
558521 /// Returns a widget showing a live camera preview.
559522 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" );
566524 try {
567525 return CameraPlatform .instance.buildPreview (_cameraId);
568526 } on PlatformException catch (e) {
@@ -572,13 +530,7 @@ class CameraController extends ValueNotifier<CameraValue> {
572530
573531 /// Gets the maximum supported zoom level for the selected camera.
574532 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" );
582534 try {
583535 return CameraPlatform .instance.getMaxZoomLevel (_cameraId);
584536 } on PlatformException catch (e) {
@@ -588,13 +540,7 @@ class CameraController extends ValueNotifier<CameraValue> {
588540
589541 /// Gets the minimum supported zoom level for the selected camera.
590542 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" );
598544 try {
599545 return CameraPlatform .instance.getMinZoomLevel (_cameraId);
600546 } on PlatformException catch (e) {
@@ -608,13 +554,7 @@ class CameraController extends ValueNotifier<CameraValue> {
608554 /// zoom level returned by the `getMaxZoomLevel` . Throws an `CameraException`
609555 /// when an illegal zoom level is suplied.
610556 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" );
618558 try {
619559 return CameraPlatform .instance.setZoomLevel (_cameraId, zoom);
620560 } on PlatformException catch (e) {
@@ -666,13 +606,7 @@ class CameraController extends ValueNotifier<CameraValue> {
666606
667607 /// Gets the minimum supported exposure offset for the selected camera in EV units.
668608 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" );
676610 try {
677611 return CameraPlatform .instance.getMinExposureOffset (_cameraId);
678612 } on PlatformException catch (e) {
@@ -682,13 +616,7 @@ class CameraController extends ValueNotifier<CameraValue> {
682616
683617 /// Gets the maximum supported exposure offset for the selected camera in EV units.
684618 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" );
692620 try {
693621 return CameraPlatform .instance.getMaxExposureOffset (_cameraId);
694622 } on PlatformException catch (e) {
@@ -700,13 +628,7 @@ class CameraController extends ValueNotifier<CameraValue> {
700628 ///
701629 /// Returns 0 when the camera supports using a free value without stepping.
702630 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" );
710632 try {
711633 return CameraPlatform .instance.getExposureOffsetStepSize (_cameraId);
712634 } on PlatformException catch (e) {
@@ -726,13 +648,7 @@ class CameraController extends ValueNotifier<CameraValue> {
726648 ///
727649 /// Returns the (rounded) offset value that was set.
728650 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" );
736652 // Check if offset is in range
737653 List <double > range =
738654 await Future .wait ([getMinExposureOffset (), getMaxExposureOffset ()]);
@@ -834,4 +750,19 @@ class CameraController extends ValueNotifier<CameraValue> {
834750 await CameraPlatform .instance.dispose (_cameraId);
835751 }
836752 }
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+ }
837768}
0 commit comments