Reputation: 2837
I have the following in the build widget and because it's in a stream it's always calling the callback, how do I stop the stream once there is data so it only happens once. I tried to dispose of it but that did not work. I also don't have a cancel method.
// Conroller and handles the listening
void _onQRViewCreated(QRViewController controller) {
setState(() {
this.controller = controller;
});
// Might need to add onError
controller.scannedDataStream.listen((scanData) {
setState(() {
result = scanData;
widget.qrSuccess(result.code);
});
});
}
Upvotes: 3
Views: 1574
Reputation: 46
Just use first
property of stream
void _onQRViewCreated(QRViewController controller) async{
setState(() {
this.controller = controller;
});
final scanData = await controller.scannedDataStream.first;
controller.scannedDataStream.close();
setState(() {
result = scanData;
widget.qrSuccess(result.code);
});
}
Upvotes: 0
Reputation: 2837
I solved this by pausing the camera here is the code
controller.scannedDataStream.listen(
(onData) {
setState(() {
controller.pauseCamera();
result = onData;
widget.qrSuccess(result.code);
print(result.code);
});
},
onError: (err) {
print('Error!');
},
cancelOnError: false,
onDone: () {
print('Done!');
},
);
Upvotes: 3