Committed by
GitHub
Merge pull request #808 from navaronbracke/unhandled_platform_exception
fix: Handle unhandled exception from the state call
Showing
2 changed files
with
43 additions
and
5 deletions
| @@ -2,6 +2,8 @@ import 'dart:async'; | @@ -2,6 +2,8 @@ import 'dart:async'; | ||
| 2 | 2 | ||
| 3 | import 'package:flutter/foundation.dart'; | 3 | import 'package:flutter/foundation.dart'; |
| 4 | import 'package:flutter/material.dart'; | 4 | import 'package:flutter/material.dart'; |
| 5 | +import 'package:flutter/services.dart'; | ||
| 6 | +import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart'; | ||
| 5 | import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; | 7 | import 'package:mobile_scanner/src/mobile_scanner_controller.dart'; |
| 6 | import 'package:mobile_scanner/src/mobile_scanner_exception.dart'; | 8 | import 'package:mobile_scanner/src/mobile_scanner_exception.dart'; |
| 7 | import 'package:mobile_scanner/src/objects/barcode_capture.dart'; | 9 | import 'package:mobile_scanner/src/objects/barcode_capture.dart'; |
| @@ -138,11 +140,31 @@ class _MobileScannerState extends State<MobileScanner> | @@ -138,11 +140,31 @@ class _MobileScannerState extends State<MobileScanner> | ||
| 138 | widget.onStart?.call(arguments); | 140 | widget.onStart?.call(arguments); |
| 139 | widget.onScannerStarted?.call(arguments); | 141 | widget.onScannerStarted?.call(arguments); |
| 140 | }).catchError((error) { | 142 | }).catchError((error) { |
| 141 | - if (mounted) { | ||
| 142 | - setState(() { | ||
| 143 | - _startException = error as MobileScannerException; | ||
| 144 | - }); | 143 | + if (!mounted) { |
| 144 | + return; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + if (error is MobileScannerException) { | ||
| 148 | + _startException = error; | ||
| 149 | + } else if (error is PlatformException) { | ||
| 150 | + _startException = MobileScannerException( | ||
| 151 | + errorCode: MobileScannerErrorCode.genericError, | ||
| 152 | + errorDetails: MobileScannerErrorDetails( | ||
| 153 | + code: error.code, | ||
| 154 | + message: error.message, | ||
| 155 | + details: error.details, | ||
| 156 | + ), | ||
| 157 | + ); | ||
| 158 | + } else { | ||
| 159 | + _startException = MobileScannerException( | ||
| 160 | + errorCode: MobileScannerErrorCode.genericError, | ||
| 161 | + errorDetails: MobileScannerErrorDetails( | ||
| 162 | + details: error, | ||
| 163 | + ), | ||
| 164 | + ); | ||
| 145 | } | 165 | } |
| 166 | + | ||
| 167 | + setState(() {}); | ||
| 146 | }); | 168 | }); |
| 147 | } | 169 | } |
| 148 | 170 |
| @@ -185,8 +185,24 @@ class MobileScannerController { | @@ -185,8 +185,24 @@ class MobileScannerController { | ||
| 185 | 185 | ||
| 186 | // Check authorization status | 186 | // Check authorization status |
| 187 | if (!kIsWeb) { | 187 | if (!kIsWeb) { |
| 188 | - final MobileScannerState state = MobileScannerState | 188 | + final MobileScannerState state; |
| 189 | + | ||
| 190 | + try { | ||
| 191 | + state = MobileScannerState | ||
| 189 | .values[await _methodChannel.invokeMethod('state') as int? ?? 0]; | 192 | .values[await _methodChannel.invokeMethod('state') as int? ?? 0]; |
| 193 | + } on PlatformException catch (error) { | ||
| 194 | + isStarting = false; | ||
| 195 | + | ||
| 196 | + throw MobileScannerException( | ||
| 197 | + errorCode: MobileScannerErrorCode.genericError, | ||
| 198 | + errorDetails: MobileScannerErrorDetails( | ||
| 199 | + code: error.code, | ||
| 200 | + details: error.details as Object?, | ||
| 201 | + message: error.message, | ||
| 202 | + ), | ||
| 203 | + ); | ||
| 204 | + } | ||
| 205 | + | ||
| 190 | switch (state) { | 206 | switch (state) { |
| 191 | case MobileScannerState.undetermined: | 207 | case MobileScannerState.undetermined: |
| 192 | bool result = false; | 208 | bool result = false; |
-
Please register or login to post a comment