简体   繁体   English

ValueListenableBuilder 不重建 CheckboxListTile Flutter

[英]ValueListenableBuilder not rebuilding a CheckboxListTile Flutter

I am trying to run a method that updates the value of a CheckboxListTile as I pass the new values at the end at Globals.data.updateFilterSelection(newFilters);我正在尝试运行一个方法来更新CheckboxListTile的值,因为我在Globals.data.updateFilterSelection(newFilters); the method runs fine and it do updates (tested it with prints), but the ValueListenableBuilder is not rebuilding the CheckboxListTile when I change its value.该方法运行良好并且它会更新(用打印测试它),但是当我更改它的值时, ValueListenableBuilder不会重建CheckboxListTile

I have three CheckboxListTile with the same code but different logic all of them listening to Globals.data.filterSelection,我有三个具有相同代码但逻辑不同的CheckboxListTile ,它们都在监听Globals.data.filterSelection,

What I am missing?我错过了什么?

Hi here is the code:您好,代码如下:

       ValueListenableBuilder<Map>(
            valueListenable: Globals.data.filterSelection,
            builder: (context, value, _) {
              return CheckboxListTile(
                activeColor: Theme.of(context).indicatorColor,
                value: value['all_neighbors'],
                secondary: Icon(
                  Icons.people,
                  color: Theme.of(context).indicatorColor,
                ),
                title: Text(
                  'All Neighbors',
                  style: Theme.of(context).textTheme.bodyText1,
                ),
                onChanged: (newValue) {
                  if (newValue) {
                    newFilters
                      ..update('inactive', (value) => false)
                      ..update('active', (value) => false)
                      ..update('all_neighbors', (value) => true);
                  } else {
                    newFilters
                      ..update('inactive', (value) => true)
                      ..update('all_neighbors', (value) => false);
                  }
                  Globals.data.updateFilterSelection(newFilters);
                },
              );
            }),

Here is also my ValueNotifier and the method called:这也是我的ValueNotifier和调用的方法:

  ValueNotifier<Map> filterSelection = ValueNotifier<Map>({
    'inactive': true,
    'active': false,
    'all_neighbors': false,
  });

  /// Changes the filter selection
  void updateFilterSelection(Map newFilter) {
    filterSelection.value = newFilter;
    print(filterSelection.value);
  }

Thanks in advance提前致谢

I found out that the ValueListenableBuilder does not rebuild on <Map> types it needs to be a single value that can be compared with the == operator, as described.我发现ValueListenableBuilder不会在<Map>类型上重建,它需要是一个可以与==运算符进行比较的单个值,如所述。

When value is replaced with something that is not equal to the old value as evaluated by the equality operator ==, this class notifies its listeners.当值被替换为不等于由相等运算符 == 评估的旧值时,此 class 会通知其侦听器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM