ValueListenableBuilder不重建CheckboxListTile Flutter

时间:2020-06-29 17:54:00

标签: flutter dart flutter-provider

我试图运行一种方法来更新CheckboxListTile的值,因为我在Globals.data.updateFilterSelection(newFilters);的末尾传递了新值,该方法运行良好并且可以更新(已通过打印测试) ,但是当我更改其值时,ValueListenableBuilder并没有重建CheckboxListTile

我有三个CheckboxListTile,它们的代码相同,但是逻辑不同,他们都在听Globals.data.filterSelection,

我缺少什么?

嗨,这是代码:

       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);
                },
              );
            }),

这也是我的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);
  }

预先感谢

1 个答案:

答案 0 :(得分:1)

我发现ValueListenableBuilder不能在<Map>类型上重建,它必须是可以与==运算符进行比较的单个值,如所述。

当用相等运算符==评估的值替换为不等于旧值的值时,此类会通知其侦听器。