[英]Flutter Error: A value of type 'BluetoothDevice?' can't be assigned to a variable of type 'BluetoothDevice'
Flutter Error: A value of type 'BluetoothDevice?' Flutter 错误:'BluetoothDevice' 类型的值? can't be assigned to a variable of type 'BluetoothDevice'.不能分配给“BluetoothDevice”类型的变量。 Try changing the type of the variable, or casting the right-hand type to 'BluetoothDevice'.尝试更改变量的类型,或将右侧类型转换为“BluetoothDevice”。
void initState() {
super.initState();
// Start scanning for devices
flutterBlue.startScan(timeout: Duration(seconds: 4));
// Listen for scan results
flutterBlue.scanResults.listen((results) {
// Find the heart rate device
device = results.firstWhere((r) => r.device.name == 'Heart Rate',
orElse: () => null)?.device;
if (device != null) {
flutterBlue.stopScan();
connectToDevice();
}
});
}
Try this:尝试这个:
void initState() {
super.initState();
// Start scanning for devices
flutterBlue.startScan(timeout: Duration(seconds: 4));
// Listen for scan results
flutterBlue.scanResults.listen((results) {
if(results != null) {
// Find the heart rate device
device = results.firstWhere((r) => r.device.name == 'Heart Rate',
orElse: () => null)!.device;
}
if (device != null) {
flutterBlue.stopScan();
connectToDevice();
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.