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 (将#修改为@)