Flutter ChangeNotifier 和 ValueNotifier 的区别

Flutter ChangeNotifier 和 ValueNotifier 的区别

  • ChangeNotifier 和 ValueNotifier 是两种用于状态管理工具,两者均基于发布-订阅模式设计 采用观察者模式
  • ChangeNotifier 需要自定义状态变量,而 ValueNotifier 可以直接使用内置 value 属性存储状态

ChangeNotifier

  • ChangeNotifier 实现了 Listenable 接口,用于管理复杂状态,需要手动调用 notifyListeners 函数触发通知(通过 addListener、removeListener 添加移除监听器)
  • 当值实际发生变化时,可以通过调用 notifyListeners 函数通知所有监听器
  • 适用于需要管理多个属性状态或是较为复杂的状态变化逻辑的场景,比如表单验证、多组件联动(用户登录状态发生变化,需要更新多个页面的显示)
dart 复制代码
//Model 用于管理状态并触发通知
class CounterNotifier with ChangeNotifier { //mixin
//class CounterNotifier extends ChangeNotifier {
  int _count = 0; //私有字段

  //提供只读属性
  int get count => _count;

  void increment() {
    _count++; //修改状态
    notifyListeners(); //手动触发更新,通知所有监听者
  }
}

class CounterNotifierStatefulWidget extends StatefulWidget {
  const CounterNotifierStatefulWidget({super.key});

  @override
  State<CounterNotifierStatefulWidget> createState() => _CounterNotifierStatefulWidgetState();
}

class _CounterNotifierStatefulWidgetState extends State<CounterNotifierStatefulWidget> {
  //
  final CounterNotifier _counterNotifier = CounterNotifier();

  @override
  void initState() {
    super.initState();
    _counterNotifier.addListener(() {
      print('addListener Callback');
      setState(() {}); //触发 UI 更新
    });
  }

  @override
  Widget build(BuildContext context) {
    print('build');
    return Scaffold(
      appBar: AppBar(
        title: const Text('ChangeNotifier 示例'),
      ),
      body: Center(
          child: Text(
        'count: ${_counterNotifier.count}',
      )),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _counterNotifier.increment(),
        child: const Icon(Icons.add),
      ),
    );
  }

  @override
  void dispose() {
    _counterNotifier.dispose();
    super.dispose();
  }
}

ValueNotifier

  • ValueNotifier 继承自 ChangeNotifier 并实现了 ValueListenable 接口,专门用于管理单个值的状态,在设置新值时,如果新值与旧值不同,ValueNotifier 内部会自动调用 ChangeNotifier#notifyListeners 函数触发通知
  • ValueNotifier 内部维护一个 value 值,可以通过这个 value 直接操作值,然后结合 ValueListenableBuilder 实现 UI 的自动更新
  • 适用于单个值的简单状态管理,比如主题切换、计数器
  • 局部刷新:利用 ValueListenableBuilder 仅重建依赖该值的组件,避免直接 setState 重建整个页面(ValueListenableBuilder 内部通过 setState 重建自身)
dart 复制代码
class ValueNotifierStatefulWidget extends StatefulWidget {
  const ValueNotifierStatefulWidget({Key? key}) : super(key: key);

  @override
  State<ValueNotifierStatefulWidget> createState() => _ValueNotifierStatefulWidgetState();
}

class _ValueNotifierStatefulWidgetState extends State<ValueNotifierStatefulWidget> {
  //
  final ValueNotifier<int> _myValueNotifier = ValueNotifier<int>(0); //初始值为 0
  
  @override
  Widget build(BuildContext context) {
    print('build');
    return Scaffold(
      appBar: AppBar(
        title: const Text('ValueNotifier 示例'),
      ),
      body: Center(
          //ValueListenableBuilder 继承自 StatefulWidget
          child: ValueListenableBuilder<int>(
              valueListenable: _myValueNotifier, //valueListenable 表示一个可监听的数据源
              builder: (BuildContext context, int value, Widget? child) {
                print('builder');
                //builder 只会在 value 值变化时被调用,此处的 value 就是 _myValueNotifier 管理的值发生变化后的新值
                return Text('count: ${_myValueNotifier.value}');
              })),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _myValueNotifier.value++; //数据变化时触发 ValueListenableBuilder 重新构建
        },
        child: const Icon(Icons.add),
      ),
    );
  }

  @override
  void dispose() {
    _myValueNotifier.dispose();
    super.dispose();
  }
}

总结

  • 单值简单状态优先采用 ValueNotifier,简单直观,多值复杂状态用 ChangeNotifier,更灵活,支持复杂逻辑扩展自定义,更适合复杂状态管理
  • 对于多个关联值,应该改用 ChangeNotifier,而非多个 ValueNotifier 实现
相关推荐
好的佩奇1 小时前
蓝牙之设备角色与连接
android·flutter·蓝牙
SoaringHeart1 天前
Flutter最佳实践:用 flutter_html 渲染富文本
前端·flutter
恋猫de小郭2 天前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter
程序员老刘2 天前
跨平台开发地图 | 2026年11月
flutter·ai编程·客户端
传奇开心果编程3 天前
【Flutter踩坑排雷】第30课:异常未捕获坑|未捕获异常直接闪退,全局兜底故障处理
android·flutter·ios·移动开发·dart·ui框架·声明式ui
恋猫de小郭3 天前
Flutter Native Interop 稳定了,Platform Channel 要退场了
android·前端·flutter
好的佩奇3 天前
Dart 之可迭代对象与生成器
android·flutter·dart
恋猫de小郭4 天前
Flutter Analyzer 系列坑推进,必看,AI 时代这玩意最吃你内存
android·前端·flutter
LinXunFeng4 天前
拿竞品 App 一扫,技术栈全暴露了
android·flutter·开源
传奇开心果编程5 天前
【Flutter踩坑排雷】第28课:平台目录缺失与修复坑|`flutter create` 的“重建”与“覆盖”边界
android·flutter·ios·移动开发·跨平台·dart·声明式ui