Committed by
GitHub
Improvements (#208)
* MobileScanner starts controller from initState * _controllerHashcode made static to keep the same value on new instances * start() call removed from MobileScannerController constructor * null-safe validations added to data casting from map
Showing
2 changed files
with
14 additions
and
14 deletions
| @@ -49,6 +49,7 @@ class _MobileScannerState extends State<MobileScanner> | @@ -49,6 +49,7 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 49 | super.initState(); | 49 | super.initState(); |
| 50 | WidgetsBinding.instance.addObserver(this); | 50 | WidgetsBinding.instance.addObserver(this); |
| 51 | controller = widget.controller ?? MobileScannerController(); | 51 | controller = widget.controller ?? MobileScannerController(); |
| 52 | + if (!controller.isStarting) controller.start(); | ||
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | @override | 55 | @override |
| @@ -35,7 +35,8 @@ class MobileScannerController { | @@ -35,7 +35,8 @@ class MobileScannerController { | ||
| 35 | EventChannel eventChannel = | 35 | EventChannel eventChannel = |
| 36 | const EventChannel('dev.steenbakker.mobile_scanner/scanner/event'); | 36 | const EventChannel('dev.steenbakker.mobile_scanner/scanner/event'); |
| 37 | 37 | ||
| 38 | - int? _controllerHashcode; | 38 | + //Must be static to keep the same value on new instances |
| 39 | + static int? _controllerHashcode; | ||
| 39 | StreamSubscription? events; | 40 | StreamSubscription? events; |
| 40 | 41 | ||
| 41 | final ValueNotifier<MobileScannerArguments?> args = ValueNotifier(null); | 42 | final ValueNotifier<MobileScannerArguments?> args = ValueNotifier(null); |
| @@ -79,8 +80,6 @@ class MobileScannerController { | @@ -79,8 +80,6 @@ class MobileScannerController { | ||
| 79 | // onCancel: () => setAnalyzeMode(AnalyzeMode.none.index), | 80 | // onCancel: () => setAnalyzeMode(AnalyzeMode.none.index), |
| 80 | ); | 81 | ); |
| 81 | 82 | ||
| 82 | - start(); | ||
| 83 | - | ||
| 84 | // Listen to events from the platform specific code | 83 | // Listen to events from the platform specific code |
| 85 | events = eventChannel | 84 | events = eventChannel |
| 86 | .receiveBroadcastStream() | 85 | .receiveBroadcastStream() |
| @@ -92,22 +91,22 @@ class MobileScannerController { | @@ -92,22 +91,22 @@ class MobileScannerController { | ||
| 92 | final data = event['data']; | 91 | final data = event['data']; |
| 93 | switch (name) { | 92 | switch (name) { |
| 94 | case 'torchState': | 93 | case 'torchState': |
| 95 | - final state = TorchState.values[data as int]; | 94 | + final state = TorchState.values[data as int? ?? 0]; |
| 96 | torchState.value = state; | 95 | torchState.value = state; |
| 97 | break; | 96 | break; |
| 98 | case 'barcode': | 97 | case 'barcode': |
| 99 | - final barcode = Barcode.fromNative(data as Map); | 98 | + final barcode = Barcode.fromNative(data as Map? ?? {}); |
| 100 | barcodesController.add(barcode); | 99 | barcodesController.add(barcode); |
| 101 | break; | 100 | break; |
| 102 | case 'barcodeMac': | 101 | case 'barcodeMac': |
| 103 | barcodesController.add( | 102 | barcodesController.add( |
| 104 | Barcode( | 103 | Barcode( |
| 105 | - rawValue: (data as Map)['payload'] as String, | 104 | + rawValue: (data as Map)['payload'] as String?, |
| 106 | ), | 105 | ), |
| 107 | ); | 106 | ); |
| 108 | break; | 107 | break; |
| 109 | case 'barcodeWeb': | 108 | case 'barcodeWeb': |
| 110 | - barcodesController.add(Barcode(rawValue: data as String)); | 109 | + barcodesController.add(Barcode(rawValue: data as String?)); |
| 111 | break; | 110 | break; |
| 112 | default: | 111 | default: |
| 113 | throw UnimplementedError(); | 112 | throw UnimplementedError(); |
| @@ -138,11 +137,11 @@ class MobileScannerController { | @@ -138,11 +137,11 @@ class MobileScannerController { | ||
| 138 | // Check authorization status | 137 | // Check authorization status |
| 139 | if (!kIsWeb) { | 138 | if (!kIsWeb) { |
| 140 | MobileScannerState state = MobileScannerState | 139 | MobileScannerState state = MobileScannerState |
| 141 | - .values[await methodChannel.invokeMethod('state') as int]; | 140 | + .values[await methodChannel.invokeMethod('state') as int? ?? 0]; |
| 142 | switch (state) { | 141 | switch (state) { |
| 143 | case MobileScannerState.undetermined: | 142 | case MobileScannerState.undetermined: |
| 144 | final bool result = | 143 | final bool result = |
| 145 | - await methodChannel.invokeMethod('request') as bool; | 144 | + await methodChannel.invokeMethod('request') as bool? ?? false; |
| 146 | state = result | 145 | state = result |
| 147 | ? MobileScannerState.authorized | 146 | ? MobileScannerState.authorized |
| 148 | : MobileScannerState.denied; | 147 | : MobileScannerState.denied; |
| @@ -190,21 +189,21 @@ class MobileScannerController { | @@ -190,21 +189,21 @@ class MobileScannerController { | ||
| 190 | throw PlatformException(code: 'INITIALIZATION ERROR'); | 189 | throw PlatformException(code: 'INITIALIZATION ERROR'); |
| 191 | } | 190 | } |
| 192 | 191 | ||
| 193 | - hasTorch = startResult['torchable'] as bool; | 192 | + hasTorch = startResult['torchable'] as bool? ?? false; |
| 194 | 193 | ||
| 195 | if (kIsWeb) { | 194 | if (kIsWeb) { |
| 196 | args.value = MobileScannerArguments( | 195 | args.value = MobileScannerArguments( |
| 197 | webId: startResult['ViewID'] as String?, | 196 | webId: startResult['ViewID'] as String?, |
| 198 | size: Size( | 197 | size: Size( |
| 199 | - startResult['videoWidth'] as double, | ||
| 200 | - startResult['videoHeight'] as double, | 198 | + startResult['videoWidth'] as double? ?? 0, |
| 199 | + startResult['videoHeight'] as double? ?? 0, | ||
| 201 | ), | 200 | ), |
| 202 | hasTorch: hasTorch, | 201 | hasTorch: hasTorch, |
| 203 | ); | 202 | ); |
| 204 | } else { | 203 | } else { |
| 205 | args.value = MobileScannerArguments( | 204 | args.value = MobileScannerArguments( |
| 206 | - textureId: startResult['textureId'] as int, | ||
| 207 | - size: toSize(startResult['size'] as Map), | 205 | + textureId: startResult['textureId'] as int?, |
| 206 | + size: toSize(startResult['size'] as Map? ?? {}), | ||
| 208 | hasTorch: hasTorch, | 207 | hasTorch: hasTorch, |
| 209 | ); | 208 | ); |
| 210 | } | 209 | } |
-
Please register or login to post a comment