我在 Flutter 应用程序中使用这段代码在满足来自流的条件后导航到另一个屏幕:
void initState() {
isReady = false;
connectToDevice();
stream?.map((value) {
final currentVal = value.toString();
final currentParsedVal = int.parse(
currentVal.substring(1, currentVal.length - 1),
onError: (source) => -1,
);
print("String data $currentParsedVal");
Seziurealert();
})?.firstWhere((value) => value == 2);
}
而这是导航到另一个屏幕的 SeizureAlert 函数:
Seziurealert() {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) {
return TriggeringAlert(device: widget.device);
},
),
(route) => false,
);
}
}
Stream 正在使用服务从 BLE 设备获取值:
// BLE
final String SERVICE_UUID = "0365a300-8d69-4066-80c7-554298a6ec5e";
final String CHARACTERISTIC_UUID = "cf01c075-cb75-4dea-819e-2a79dd466bcb";
bool isReady;
Stream<List<int>> stream;
List<int> lastValue;
connectToDevice() async {
// await widget.device.connect();
discoverServices();
}
discoverServices() async {
List<BluetoothService> services = await widget.device.discoverServices();
services.forEach((service) {
if (service.uuid.toString() == SERVICE_UUID) {
service.characteristics.forEach((characteristic) {
if (characteristic.uuid.toString() == CHARACTERISTIC_UUID) {
characteristic.setNotifyValue(!characteristic.isNotifying);
stream = characteristic.value;
print(stream);
lastValue = characteristic.lastValue;
print(lastValue);
// setState(() {
// isReady = true;
// });
}
});
}
});
}
我不明白它是否在控制台上显示了值,那么为什么它没有导航到另一个屏幕?
请帮助我,因为我是 Flutter 的新手