Navaron Bracke

handle more exception types in cast

@@ -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