英文:
How can i make future function to wait for a stream to complete?
问题
Future notifyRead(Guid characteristic, BuildContext context) async {
try {
if (await FlutterBlue.instance.isOn) {
BluetoothCharacteristic charToTarget = _characteristics
.firstWhere((element) => element.uuid == characteristic);
await charToTarget.setNotifyValue(true);
clearInfo();
var stream = charToTarget.value.listen(
(value) {
var temp = String.fromCharCodes(value);
_info.add(temp);
print(temp);
notifyListeners();
if (temp.startsWith("batt")) {
updateBattValues(temp);
}
updateWifiVerificationStatus();
if (_info.length == 17) {
log("Finished Sending Data ");
_notifyFinished = true;
notifyListeners();
return;
}
},
);
await charToTarget.write(INFO.codeUnits);
// Add this part to wait for the stream to complete
await stream.drain();
} else {
Fluttertoast.showToast(msg: "Seems like your Bluetooth is turned off");
}
} catch (e) {
log("Some error occurred in retrieving info ${e.toString()}");
Fluttertoast.showToast(
msg: "Some error occurred in retrieving info ${e.toString()}");
}
}
以上是您提供的代码的翻译,我已经将其修改,以等待流完成。在流的末尾使用await stream.drain();
来等待流完成,然后才会继续执行后续代码。这会确保notifyRead
方法等待流的完成。
英文:
Future notifyRead(Guid characteristic, BuildContext context) async {
try {
if (await FlutterBlue.instance.isOn) {
BluetoothCharacteristic charToTarget = _characteristics
.firstWhere((element) => element.uuid == characteristic);
await charToTarget.setNotifyValue(true);
clearInfo();
var stream = charToTarget.value.listen(
(value) {
var temp = String.fromCharCodes(value);
_info.add(temp);
print(temp);
notifyListeners();
if (temp.startsWith("batt")) {
updateBattValues(temp);
}
updateWifiVerificationStatus();
if (_info.length == 17) {
log("Finished Sending Data ");
_notifyFinished = true;
notifyListeners();
return;
}
},
);
await charToTarget.write(INFO.codeUnits);
} else {
Fluttertoast.showToast(msg: "Seems like your Bluetooth is turned off");
}
} catch (e) {
log("Some error occurred in retrieving info ${e.toString()}");
Fluttertoast.showToast(
msg: "Some error occurred in retrieving info ${e.toString()}");
}
}
I want this notifyRead method to wait for the stream inside to complete.But I am not able to achieve the same.The stream does not have an end associated with it , all I know is the stream outputs 17 values(that I save in _info array ) , so the check the length of the data and mark the end.
This notifyRead method is called by a button's onTap.I want to show a till the stream finishes.
答案1
得分: 1
这在使用 await for
时非常简单:
void main() async {
print('before');
await asyncFunction();
print('after');
}
Future<void> asyncFunction() async {
int count = 0;
await for (var _ in streamFunction()) {
count++;
print('count = $count');
if (count == 17) {
break;
}
}
}
Stream<int> streamFunction() async* {
for (var i = 0; ; i++) {
yield i;
}
}
输出:
before
count = 1
count = 2
count = 3
count = 4
count = 5
count = 6
count = 7
count = 8
count = 9
count = 10
count = 11
count = 12
count = 13
count = 14
count = 15
count = 16
count = 17
after
英文:
This is very straightforward when using await for
:
void main() async {
print('before');
await asyncFunction();
print('after');
}
Future<void> asyncFunction() async {
int count = 0;
await for (var _ in streamFunction()) {
count++;
print('count = $count');
if (count == 17) {
break;
}
}
}
Stream<int> streamFunction() async* {
for (var i = 0; ; i++) {
yield i;
}
}
Output:
before
count = 1
count = 2
count = 3
count = 4
count = 5
count = 6
count = 7
count = 8
count = 9
count = 10
count = 11
count = 12
count = 13
count = 14
count = 15
count = 16
count = 17
after
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论