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