Julian Steenbakker

Merge branch 'hotfix/usage-after-dispose' into hotfix/hot-reload

# Conflicts:
#	lib/src/mobile_scanner.dart
... ... @@ -22,10 +22,11 @@ class ScannerErrorWidget extends StatelessWidget {
error.errorCode.message,
style: const TextStyle(color: Colors.white),
),
if (error.errorDetails != null) Text(
error.errorDetails!.message ?? '',
style: const TextStyle(color: Colors.white),
),
if (error.errorDetails?.message case final String message)
Text(
message,
style: const TextStyle(color: Colors.white),
),
],
),
),
... ...
... ... @@ -83,7 +83,7 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
throw MobileScannerException(
errorCode: MobileScannerErrorCode.unsupported,
errorDetails: MobileScannerErrorDetails(
message:MobileScannerErrorCode.unsupported.message,
message: MobileScannerErrorCode.unsupported.message,
),
);
}
... ...
... ... @@ -202,7 +202,6 @@ class _MobileScannerState extends State<MobileScanner>
return ValueListenableBuilder<MobileScannerState>(
valueListenable: controller,
builder: (BuildContext context, MobileScannerState value, Widget? child) {
// If the controller is still initializing, show a black screen, or user provided placeholder
if (!value.isInitialized) {
const Widget defaultPlaceholder = ColoredBox(color: Colors.black);
... ... @@ -211,7 +210,6 @@ class _MobileScannerState extends State<MobileScanner>
}
final MobileScannerException? error = value.error;
// If the controller encountered, show an error screen, or user provided placeholder
if (error != null) {
final Widget defaultError = ScannerErrorWidget(error: error);
... ... @@ -260,7 +258,7 @@ class _MobileScannerState extends State<MobileScanner>
StreamSubscription? _subscription;
Future<void> initController() async {
Future<void> initMobileScanner() async {
// If debug mode is enabled, stop the controller first before starting it.
// If a hot-restart is initiated, the controller won't be stopped, and because
// there is no way of knowing if a hot-restart has happened, we must assume
... ... @@ -290,7 +288,10 @@ class _MobileScannerState extends State<MobileScanner>
}
}
Future<void> disposeCamera() async {
Future<void> disposeMobileScanner() async {
await _subscription?.cancel();
WidgetsBinding.instance.removeObserver(this);
if (controller.autoStart) {
await controller.stop();
}
... ... @@ -298,7 +299,6 @@ class _MobileScannerState extends State<MobileScanner>
// Dispose default controller if not provided by user
if (widget.controller == null) {
await controller.dispose();
WidgetsBinding.instance.removeObserver(this);
}
}
... ... @@ -306,19 +306,13 @@ class _MobileScannerState extends State<MobileScanner>
void initState() {
super.initState();
controller = widget.controller ?? MobileScannerController();
initController();
unawaited(initMobileScanner());
}
@override
void dispose() {
if (_subscription != null) {
_subscription!.cancel();
_subscription = null;
}
disposeCamera();
super.dispose();
disposeMobileScanner();
}
@override
... ... @@ -342,7 +336,6 @@ class _MobileScannerState extends State<MobileScanner>
unawaited(controller.start());
case AppLifecycleState.inactive:
unawaited(_subscription?.cancel());
_subscription = null;
unawaited(controller.stop());
}
}
... ...
... ... @@ -176,8 +176,7 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> {
throw MobileScannerException(
errorCode: MobileScannerErrorCode.controllerDisposed,
errorDetails: MobileScannerErrorDetails(
message:
MobileScannerErrorCode.controllerDisposed.message,
message: MobileScannerErrorCode.controllerDisposed.message,
),
);
}
... ...
... ... @@ -20,18 +20,20 @@ class ScannerErrorWidget extends StatelessWidget {
child: Icon(Icons.error, color: Colors.white),
),
if (kDebugMode) ...[
Text(
error.errorCode.message,
style: const TextStyle(color: Colors.white),
),
if (error.errorDetails != null) Text(
error.errorDetails!.message ?? '',
style: const TextStyle(color: Colors.white),
),
] else Text(
MobileScannerErrorCode.genericError.message,
style: const TextStyle(color: Colors.white),
),
Text(
error.errorCode.message,
style: const TextStyle(color: Colors.white),
),
if (error.errorDetails?.message case final String message)
Text(
message,
style: const TextStyle(color: Colors.white),
),
] else
Text(
MobileScannerErrorCode.genericError.message,
style: const TextStyle(color: Colors.white),
),
],
),
),
... ...