简体   繁体   English

Flutter“字符串”不是“列表”类型的子类型<int> '</int>

[英]Flutter 'String' is not a subtype of type 'List<int>'

I am trying to subscribe to my BLE device characteristic and print the notified values on my phones screen.我正在尝试订阅我的 BLE 设备特性并在我的手机屏幕上打印通知值。 I know the code is horrible, but this is my first flutter project so bear with me please.我知道代码很糟糕,但这是我的第一个 flutter 项目,所以请多多包涵。

The error comes from the StreamBuilder widget and the problem should be utf8.decode(snapshot.data)错误来自 StreamBuilder 小部件,问题应该是utf8.decode(snapshot.data)

I would also happily accept any ideas on how to make the code easier to read and more efficient, since this right here for the moment is the best I can do.我也很乐意接受有关如何使代码更易于阅读和更高效的任何想法,因为目前这是我能做的最好的。

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.
        );
      }
    }

It seems you are trying to pass String value into a List having Integer format.您似乎正在尝试将字符串值传递到具有 Integer 格式的列表中。 Change the List type from Integer to String.将列表类型从 Integer 更改为字符串。 This could solve the issue.这可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 错误:“int”类型不是类型转换中“String”类型的子类型 - Flutter error: type 'int' is not a subtype of type 'String' in type cast 使用 tflite 时,“String”类型不是“index”类型“int”的子类型 - flutter - type 'String' is not a subtype of type 'int' of 'index' while using tflite - flutter Flutter 错误:异常:“int”类型不是“String”类型的子类型 - Flutter Error : Exception: type 'int' is not a subtype of type 'String' 红屏错误类型“字符串”不是“索引”Flutter 的“int”类型的子类型 - Red Screen Error type 'String' is not a subtype of type 'int' of 'index' Flutter 未处理的异常:类型&#39;String&#39;不是颤振中&#39;index&#39;的&#39;int&#39;类型的子类型 - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' in flutter 颤振:输入&#39;列表<dynamic> &#39; 不是 &#39;List 类型的子类型<DropdownMenuItem<String> &gt;&#39; - flutter: type 'List<dynamic>' is not a subtype of type 'List<DropdownMenuItem<String>>' int 类型不是 String 类型的子类型 - Type int is not a subtype of type String _TypeError(类型为“列表” <dynamic> &#39;不是&#39;Map类型的子类型 <String, dynamic> &#39;)扑 - _TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>') flutter 类型“String”不是类型转换中“int”类型的子类型 - Type 'String' is not a subtype of type 'int' in type cast 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型转换 int flutter 中类型 'OriginDestination' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'OriginDestination' in type cast int flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM