In my Flutter app I want to scan some barcodes. After scanning the barcode I want to show a snackbar. The app should run on an Android device.
Unfortunately the snackbar never shows up. Both functionalities (barcode scanning and showing the snackbar) work fine separately.
During debugging, following happens:
- The camera opens
- The barcode is scanned
- The barcode is stored in the variable
- The screen turns black
- The code for the snack bar is executed. The snack bar is not displayed.
- After leaving the method the view/widget on which the snack bar is to be shownn is rendered again.
The barcode scanning is handled by the flutter plugin barcode_scan in version 0.0.3.
Here is the matching code:
final GlobalKey<ScaffoldState> keyScaffold = new GlobalKey<ScaffoldState>(); [...] @override Widget build(BuildContext context) { return new Scaffold( key: keyScaffold, [...] } Future scan() async { String snackBarMessage = "No error"; try { String barcode = await BarcodeScanner.scan(); setState(() => _barcodeController.text = barcode); snackBarMessage = barcode; } on PlatformException catch (e) { if (e.code == BarcodeScanner.CameraAccessDenied) { snackBarMessage = 'The user did not grant the camera permission!'; } else { snackBarMessage = 'Unknown error: $e'; } } on FormatException { snackBarMessage = 'null (User returned using the "back"-button before scanning anything. Result)'; } catch (e) { snackBarMessage = 'Unknown error: $e'; } keyScaffold.currentState.showSnackBar(new SnackBar( content: new Text(snackBarMessage), duration: new Duration(seconds: 5), )); }