Reputation: 641
Hi I'm trying to read a QR code and send the data in the QR code to my server. But when running flutter build ios
I get the error in xcode when launching the app:
LateInitializationError: Field '_channel@598294394' has not been initialized.
#0 _QRViewState._channel (package:qr_code_scanner/src/qr_code_scanner.dart)
#1 _QRViewState.updateDimensions (package:qr_code_scanner/src/qr_code_scanner.dart:91:57)
#2 LifecycleEventHandler.didChangeAppLifecycleState (package:qr_code_scanner/src/lifecycle_event_handler.dart:15:29)
#3 WidgetsBinding.handleAppLifecycleStateChanged (package:flutter/src/widgets/binding.dart:692:16)
#4 ServicesBinding._handleLifecycleMessage (package:flutter/src/services/binding.dart:192:5)
#5 BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:73:49)
#6 BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:72:47)
#7 _DefaultBinaryMessenger.handlePlatformMessage (package:flutter/src/services/binding.dart:284:33)
#8 _invoke3.<anonymous closure> (dart:ui/hooks.dart:223:15)
#9 _rootRun (dart:async/zone.dart:1354:13)
#10 _CustomZone.run (dart:async/zone.dart:1258:19)
#11 _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
#12 _invoke3 (dart:ui/hooks.dart:222:10)
#13 PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:520:7)
#14 _dispatchPlatformMessage (dart:ui/hooks.dart:90:31)
The same thing happens when running flutter run
But that is fixed by doing a hot restart. Does anybody know why this is happening?
When this happens the app refuses to read any QR codes.
You can find my code on github here: https://github.com/maxall41/Package-Scanner
Upvotes: 2
Views: 6182
Reputation: 605
This is happening because you are calling setState before your widget has fully initialized itself. You can't call set state before the build method has finished because there is nothing to set the state of.
When you do a hot restart the phone (or emulator) keeps the state of the page or widget and rebuilds the ui. At that point, the build method of the widget gets called again and because your set state is in your build method, it is also getting called again, but this time on a state that was already initialized.
As a side note: Please post the relevant code instead of a link to your github repo. It helps you get better answers and it makes this a more useful question/ answer to the community
Edit: Here is the block of code causing problems.
Widget _buildQrView(BuildContext context) {
return QRView(
key: qrKey,
onQRViewCreated: _onQRViewCreated,
);
}
void _onQRViewCreated(QRViewController controller) {
//Here's the setState in the build method
setState(() {
this.controller = controller;
});
controller.scannedDataStream.listen((scanData) async {
setState(() {
result = "Scanned: " + scanData.code;
});
});
}
Removing the setState
surrounding this.controller = controller
should solve the problem
Upvotes: 5