窗口小部件无法从监听器扑动中获得更新

问题描述 投票:0回答:1

我已通过以下方式实现了侦听器:

  @override
  void didChangeDependencies() {
    final SlotDataProvider slotDevice = Provider.of<SlotDataProvider>(context);  
    spptask.streamdevice.listen((device) {
      setState(() {
        slotDevice._devices[0].name = device.name;
        print("Device data received: ${device.name} ");
      });
    }, onError: (error) {
      print("Error: $error.message");
    });

    super.didChangeDependencies();
  }

我在分离的控制器上侦听,并调用了打印“设备数据已接收:...”,但未实现该小部件。在构建方法中,请执行以下操作:

            ...
  @override
  Widget build(BuildContext context) {
    final slotProvider = Provider.of<SlotDataProvider>(context);
    final deviceProvider = Provider.of<DeviceDataProvider>(context);

    Device slotDevice = slotProvider.getDevice(widget.slot);
    Device device = deviceProvider.getDevice(widget.slot);

    _dropdownMenuItems = buildDropdownMenuItems(deviceProvider.get());
    return ListTile(
      title: Row(
        children: <Widget>[
          SizedBox(
            width: 140,
            child: DropdownButton(
                isExpanded: true,
                disabledHint: Text(slotDevice.name),
                hint: Text(slotDevice.name),
                value: device,
                items: _dropdownMenuItems,
                onChanged: (value) {
                  device.setDevice(value);
                  slotDevice.setDevice(value);
                }),
          ),
          SizedBox(width: 10),
          SizedBox(width: 60, child: Text('SLOT#${slotDevice.slot}')),
        ],
      ),
      subtitle: Text(slotDevice.bdaddr, style: TextStyle(fontSize: 10.0)),
      leading: SizedBox(
        height: 40,
        width: 35,
        child: UsbBatteryImageAsset(slot: widget.slot),
      ),
      trailing: Icon(Icons.keyboard_arrow_right),
    );
  }
}

以上代码中缺少什么。 SlotDataProvider是“设备”的修复列表,具有名称,ID等属性。

@ EDIT问题与组合框有关。如果我更改其他字段,它会起作用。

flutter dart notifications widget provider
1个回答
0
投票

通常根据更新的数据来重建小部件,我们使用streambuilders每当流中发生更改时,这将导致小部件重建它表明您的小部件是在第一次监听数据的情况下构建的一次

您是否尝试过将Gridview包装在有状态的构建器中?

© www.soinside.com 2019 - 2024. All rights reserved.