如何解决flutter:使用流而不是setState在单独的类中更新值状态?
我目前正在构建一个应用程序,该应用程序从单独的类中获取数据。 列表设备装满后,应将容器添加到可扩展列表中。 我已经设法做到这一点,但是..导致设备列表的状态在更改时不会更新,UI也不会添加容器。而且我不能在单独的类中使用set状态。
void initState() {
// TODO: implement initState
super.initState();
getDevices();
}
Future<void> getDevices() async {
setState(() async {
deviceLength = drs.devices.length;
await drs.allDevices(specificDevices: 'DRS');
});
}
设置状态仅在启动时更新deviceLength,但不会继续。
设备的长度由单独的蓝牙类中的以下流定义,该类将对象添加到列表设备:
Future<Stream<void>> _findSingleResult({searchFor: ''}) async {
_flutterBlue.scanResults.listen((results) async {
// Return found Devices
try {
print('Device Found ? $deviceFound');
print(results.last.device.name);
//Does found device equal searched Device?
if (results.last.device.name.contains(searchFor)) {
deviceFound = true;
devices.add(results.last.device);
print('Device found...');
await _flutterBlue.stopScan();
return;
}
_counter++;
} catch (e) {
print(e);
}
});
}
如果有人知道如何解决此问题,请帮忙:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。