Julian Steenbakker

fix: move subscription.cancel to async dispose, move removeObserver

@@ -256,7 +256,7 @@ class _MobileScannerState extends State<MobileScanner> @@ -256,7 +256,7 @@ class _MobileScannerState extends State<MobileScanner>
256 256
257 StreamSubscription? _subscription; 257 StreamSubscription? _subscription;
258 258
259 - Future<void> initController() async { 259 + Future<void> initMobileScanner() async {
260 // TODO: This will be fixed in another PR 260 // TODO: This will be fixed in another PR
261 // If debug mode is enabled, stop the controller first before starting it. 261 // If debug mode is enabled, stop the controller first before starting it.
262 // If a hot-restart is initiated, the controller won't be stopped, and because 262 // If a hot-restart is initiated, the controller won't be stopped, and because
@@ -284,7 +284,10 @@ class _MobileScannerState extends State<MobileScanner> @@ -284,7 +284,10 @@ class _MobileScannerState extends State<MobileScanner>
284 } 284 }
285 } 285 }
286 286
287 - Future<void> disposeCamera() async { 287 + Future<void> disposeMobileScanner() async {
  288 + await _subscription?.cancel();
  289 + WidgetsBinding.instance.removeObserver(this);
  290 +
288 if (controller.autoStart) { 291 if (controller.autoStart) {
289 await controller.stop(); 292 await controller.stop();
290 } 293 }
@@ -292,7 +295,6 @@ class _MobileScannerState extends State<MobileScanner> @@ -292,7 +295,6 @@ class _MobileScannerState extends State<MobileScanner>
292 // Dispose default controller if not provided by user 295 // Dispose default controller if not provided by user
293 if (widget.controller == null) { 296 if (widget.controller == null) {
294 await controller.dispose(); 297 await controller.dispose();
295 - WidgetsBinding.instance.removeObserver(this);  
296 } 298 }
297 } 299 }
298 300
@@ -300,19 +302,13 @@ class _MobileScannerState extends State<MobileScanner> @@ -300,19 +302,13 @@ class _MobileScannerState extends State<MobileScanner>
300 void initState() { 302 void initState() {
301 super.initState(); 303 super.initState();
302 controller = widget.controller ?? MobileScannerController(); 304 controller = widget.controller ?? MobileScannerController();
303 - initController(); 305 + initMobileScanner();
304 } 306 }
305 307
306 @override 308 @override
307 void dispose() { 309 void dispose() {
308 - if (_subscription != null) {  
309 - _subscription!.cancel();  
310 - _subscription = null;  
311 - }  
312 -  
313 - disposeCamera();  
314 -  
315 super.dispose(); 310 super.dispose();
  311 + disposeMobileScanner();
316 } 312 }
317 313
318 @override 314 @override
@@ -336,7 +332,6 @@ class _MobileScannerState extends State<MobileScanner> @@ -336,7 +332,6 @@ class _MobileScannerState extends State<MobileScanner>
336 unawaited(controller.start()); 332 unawaited(controller.start());
337 case AppLifecycleState.inactive: 333 case AppLifecycleState.inactive:
338 unawaited(_subscription?.cancel()); 334 unawaited(_subscription?.cancel());
339 - _subscription = null;  
340 unawaited(controller.stop()); 335 unawaited(controller.stop());
341 } 336 }
342 } 337 }