简体   繁体   English

Flutter 错误:'BluetoothDevice' 类型的值? 不能分配给“BluetoothDevice”类型的变量

[英]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.

相关问题 如何在我的 flutter 应用程序中将蓝牙设备 Object 存储到共享首选项(不支持蓝牙设备 object 类型) - How to Store a BluetoothDevice Object to Shared Preferences in my flutter App( BluetoothDevice object type not supported) Flutter:蓝牙设备 - Flutter: Bluetoothdevice “bool?”类型的值? 不能分配给“bool”类型的变量。 flutter - A value of type 'bool?' can't be assigned to a variable of type 'bool'. flutter Flutter:“日期时间”类型的值? 不能分配给“日期时间”类型的变量 - Flutter: A value of type 'DateTime?' can't be assigned to a variable of type 'DateTime' Flutter:“ListTileThemeData”类型的值不能分配给“ListTileTheme”类型的变量 - Flutter: A value of type 'ListTileThemeData' can't be assigned to a variable of type 'ListTileTheme' Flutter:“GoogleSignInAccount”类型的值? 不能分配给“GoogleSignInAccount”类型的变量 - Flutter: A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount' 颤振错误:“对象?”类型的值不能分配给“字符串”类型的变量 - Flutter Error : A value of type 'Object?' can't be assigned to a variable of type 'String' Flutter 错误:“bool”类型的值? 不能分配给“bool”类型的变量 - Flutter Error: A value of type 'bool?' can't be assigned to a variable of type 'bool' 颤振错误“对象?”类型的值不能分配给“int”类型的变量 - Flutter error A value of type 'Object?' can't be assigned to a variable of type 'int' 错误:“未来”类型的值<widget> ' 不能分配给类型为 'Widget' flutter 的变量</widget> - Error: A value of type 'Future<Widget>' can't be assigned to a variable of type 'Widget' flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM