Navaron Bracke

do nothing in start/stop if already started/stopped

@@ -205,6 +205,8 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -205,6 +205,8 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
205 /// 205 ///
206 /// The [cameraDirection] can be used to specify the camera direction. 206 /// The [cameraDirection] can be used to specify the camera direction.
207 /// If this is null, this defaults to the [facing] value. 207 /// If this is null, this defaults to the [facing] value.
  208 + ///
  209 + /// Does nothing if the camera is already running.
208 Future<void> start({CameraFacing? cameraDirection}) async { 210 Future<void> start({CameraFacing? cameraDirection}) async {
209 if (_isDisposed) { 211 if (_isDisposed) {
210 throw const MobileScannerException( 212 throw const MobileScannerException(
@@ -216,6 +218,11 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -216,6 +218,11 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
216 ); 218 );
217 } 219 }
218 220
  221 + // Do nothing if the camera is already running.
  222 + if (value.isRunning) {
  223 + return;
  224 + }
  225 +
219 final CameraFacing effectiveDirection = cameraDirection ?? facing; 226 final CameraFacing effectiveDirection = cameraDirection ?? facing;
220 227
221 final StartOptions options = StartOptions( 228 final StartOptions options = StartOptions(
@@ -267,11 +274,18 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { @@ -267,11 +274,18 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
267 /// Stop the camera. 274 /// Stop the camera.
268 /// 275 ///
269 /// After calling this method, the camera can be restarted using [start]. 276 /// After calling this method, the camera can be restarted using [start].
  277 + ///
  278 + /// Does nothing if the camera is already stopped.
270 Future<void> stop() async { 279 Future<void> stop() async {
271 - _disposeListeners();  
272 -  
273 _throwIfNotInitialized(); 280 _throwIfNotInitialized();
274 281
  282 + // Do nothing if already stopped.
  283 + if (!value.isRunning) {
  284 + return;
  285 + }
  286 +
  287 + _disposeListeners();
  288 +
275 // After the camera stopped, set the torch state to off, 289 // After the camera stopped, set the torch state to off,
276 // as the torch state callback is never called when the camera is stopped. 290 // as the torch state callback is never called when the camera is stopped.
277 value = value.copyWith( 291 value = value.copyWith(