Dart异步不等待完成
我目前正在尝试等待BLE连接产生以下两种结果之一:Dart异步不等待完成,dart,flutter,dart-async,Dart,Flutter,Dart Async,我目前正在尝试等待BLE连接产生以下两种结果之一: 设备已成功连接,或 扫描超时后设备连接失败 不会根据需要返回true或false值,而是立即返回null,而无需等待函数完成 我使用dart的Future和async功能来等待connect功能的完成。下面是我的代码: 可扩展连接方法: static Future<bool> connect(BluetoothDevice d) async { // Connect to device Duration
- 设备已成功连接,或
- 扫描超时后设备连接失败
static Future<bool> connect(BluetoothDevice d) async {
// Connect to device
Duration timeout = new Duration(seconds: 5);
deviceConnection = _flutterBlue.connect(d, timeout: timeout).listen((s) {
deviceState = s;
if (s == BluetoothDeviceState.connected) {
device = d;
device.discoverServices().then((s) {
... Some service discovery stuff ...
});
}
}, onDone: () {
return deviceState == BluetoothDeviceState.connected;
});
}
我做错了什么?onDone部分没有达到您的预期。
请尝试:
static Future<bool> connect(BluetoothDevice d) async {
// Connect to device
Duration timeout = new Duration(seconds: 5);
await _flutterBlue.connect(d, timeout: timeout).listen((s) {
deviceState = s;
if (s == BluetoothDeviceState.connected) {
device = d;
device.discoverServices().then((s) {
... Some service discovery stuff ...
});
}
}).asFuture();
return deviceState == BluetoothDeviceState.connected;
}
静态未来连接(蓝牙设备d)异步{
//连接到设备
持续时间超时=新的持续时间(秒:5);
等待。连接(d,超时:超时)。侦听((s){
deviceState=s;
如果(s==BluetoothDeviceState.connected){
装置=d;
device.discoverServices()然后((s){
…一些服务发现的东西。。。
});
}
}).asFuture();
返回设备状态==BluetoothDeviceState.connected;
}
正如Günther Zöchbauer所指出的,错误在onDone
部分。您将返回一个没有人会看到的值,并且不会从周围函数返回任何内容
您在一个异步函数中,因此可以使用wait for
来迭代流。
您还希望在第一次收到连接事件时停止侦听流,因为您只关心第一次连接。连接事件流本身从不停止
static Future<bool> connect(BluetoothDevice d) async {
// Connect to device
Duration timeout = const Duration(seconds: 5);
await for (var s in _flutterBlue.connect(d, timeout: timeout)) {
deviceState = s;
if (s == BluetoothDeviceState.connected) {
device = d;
device.discoverServices().then((s) {
... Some service discovery stuff ...
});
return true;
}
}
// The stream ended somehow, there will be no further events.
return false;
}
还有一点值得怀疑的是,没有人等待device.discoverServices()返回的未来。然后(…)。确保这是正确的。Günter,我在我的代码中尝试了您的解决方案,但它似乎并不总是有效,我假设这是因为我的另一个误解。如果设备无法连接此解决方案,则持续时间将超时,方法将按计划返回false。但是,当设备成功连接时,函数永远不会返回,我也永远不会从函数返回bool值,因为我有方法检查返回值。可能是什么问题?您是否也检查了lrn的方法?是的,我检查过,他的方法也会产生相同的结果。为什么函数会像那样“挂起”呢?我不知道。可能
\u.connect(d,timeout:timeout)。
永远不会完成流。您可以尝试。首先
而不是asFuture()
Irn,我已经尝试了您的第一个解决方案(我希望保持此函数异步),但它似乎对我不起作用。与Gunther的解决方案一样,函数在成功连接时不会返回true,相反,它从不返回值。同样像Gunther的解决方案一样,当持续时间超时时,返回正确的值(false)。我不知道代码的意思,所以这只是猜测:当第一个s==bluetoothDeviceState.connected
成功时,是否要停止侦听?如果是这样,请在If
正文的末尾添加一个break
。这将打破循环。这非常有效。我承认我相当困惑——中断的位置对我来说是有意义的,但方法从未返回值这一事实让我困惑。谢谢你的帮助!您正在侦听一个连接事件流,只有当该流完成或您停止侦听时,侦听才会停止。我猜流从未关闭(蓝牙设备从未停止侦听连接),因此您必须退出wait for
循环来停止侦听。我将更新示例代码。
static Future<bool> connect(BluetoothDevice d) async {
// Connect to device
Duration timeout = const Duration(seconds: 5);
await for (var s in _flutterBlue.connect(d, timeout: timeout)) {
deviceState = s;
if (s == BluetoothDeviceState.connected) {
device = d;
device.discoverServices().then((s) {
... Some service discovery stuff ...
});
return true;
}
}
// The stream ended somehow, there will be no further events.
return false;
}
static Future<bool> connect(BluetoothDevice d) {
// Connect to device
Duration timeout = const Duration(seconds: 5);
return _flutterBlue.connect(d, timeout: timeout).firstWhere((s) {
return s == BluetoothDeviceState.connected;
}, orElse: () => null).then((s) {
if (s == null) return false;
deviceState = s;
device = d;
device.discoverServices().then((s) {
//... Some service discovery stuff ...
});
return true;
});