微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter 'String' 不是类型 'List<int>' 的子类型

如何解决Flutter 'String' 不是类型 'List<int>' 的子类型

我正在尝试订阅我的 BLE 设备特性并在我的手机屏幕上打印通知值。我知道代码很糟糕,但这是我的第一个 Flutter 项目,请耐心等待。

错误来自 StreamBuilder 小部件,问题应该是 utf8.decode(snapshot.data)

我也很乐意接受有关如何使代码更易于阅读和更高效的任何想法,因为目前这是我能做的最好的事情。

FlutterBlue FlutterBlue = FlutterBlue.instance;
BluetoothDevice device;
BluetoothState state;
BluetoothDeviceState deviceState;

StreamSubscription<ScanResult> subscription;
var myUUID = "11111815-0000-1000-8000-00805f9b34fb";
var myCharacteristicUUID = "00002aca-0000-1000-8000-00805f9b34fb";
var myDevice = "ESP32";
var currentValue;

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key,this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}
 
class _MyHomePageState extends State<MyHomePage> {
      void scanForDevices() async {
        if (FlutterBlue.isScanning != null) {
          print("Scanning");
          subscription = FlutterBlue
              .scan(timeout: const Duration(seconds: 4))
              .listen((scanResult) async {
            if (scanResult.device.name == myDevice) {
              print("found ESP32");
              //Assigning bluetooth device
              device = scanResult.device;
              //After that we stop the scanning for device
              stopScanning();
            }
          });
        } else {
          print("Already scanning");
        }
        print("Scan stopped");
      }
    
      void stopScanning() {
        FlutterBlue.stopScan();
        subscription.cancel();
        connectToDevice();
      }
    
      connectToDevice() async {
    //Flutter_blue makes our life easier
        await device.connect();
        print("ESP32 connected");
    //After connection start dicovering services
        discoverServices();
      }
    
      BluetoothCharacteristic c;
    //This stream is for taking characteristic's value
    //for reading data provided by device
      Stream<List<int>> listStream;
      discoverServices() async {
        print("discovering services");
        List<BluetoothService> services = await device.discoverServices();
        //checking each services provided by device
        services.forEach((service) {
          if (service.uuid.toString() == myUUID) {
            service.characteristics.forEach((characteristic) async {
              if (characteristic.uuid.toString() == myCharacteristicUUID) {
                //Updating stream to perform read operation.
                listStream = characteristic.value;
                await characteristic.setNotifyValue(!characteristic.isnotifying);
                characteristic.value.listen((value) {
                  print(utf8.decode(value));
                });
              }
            });
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),),body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
                Text("Stream:"),StreamBuilder(
                  stream: listStream,initialData: "None",builder: (context,snapshot) {
                    // if i remove "utf8.decode(snapshot.data)" the error goes away
                    return Text(utf8.decode(snapshot.data));
                  },],floatingActionButton: FloatingActionButton(
            onpressed: scanForDevices,tooltip: 'Scan',child: Icon(Icons.add),// This trailing comma makes auto-formatting nicer for build methods.
        );
      }
    }

解决方法

您似乎正在尝试将字符串值传递到具有整数格式的列表中。将列表类型从整数更改为字符串。这可以解决问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?