You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Riverpod中是否存在Provider的ChangeNotifierProvider等效Widget?

How to Create ChangeNotifierProvider Only When a Page is Pushed to Navigator & Auto-Dispose on Pop

Hey there! Let me break down exactly how to nail this behavior—you want your ChangeNotifier to spin up only when its host page gets pushed onto the Navigator stack, and automatically clean itself up when the page pops off. This is totally doable with Flutter's widget lifecycle and the provider package, here's the straightforward approach:

The Core Idea

Instead of placing your ChangeNotifierProvider high up in the widget tree (like above MaterialApp), wrap it directly inside the page widget that gets pushed. This way, the provider only gets instantiated when the page is added to the stack, and Flutter's built-in widget disposal system will handle auto-cleanup when the page is removed.

Step-by-Step Implementation

1. Define Your ChangeNotifier

First, create your notifier class as usual—make sure to extend ChangeNotifier and add any cleanup logic in dispose() if you're using resources like timers or subscriptions:

class PageNotifier extends ChangeNotifier {
  int _count = 0;
  int get count => _count;

  void incrementCount() {
    _count++;
    notifyListeners();
  }

  @override
  void dispose() {
    // Add cleanup code here (e.g., cancel timers, close streams)
    print('PageNotifier disposed successfully!');
    super.dispose();
  }
}

2. Wrap Your Page with ChangeNotifierProvider

When building the page that will be pushed, wrap its entire content with ChangeNotifierProvider using the create parameter. This ensures the notifier is created exactly when the page is built (i.e., when it's pushed to the Navigator stack):

class TargetPage extends StatelessWidget {
  const TargetPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => PageNotifier(),
      child: Scaffold(
        appBar: AppBar(title: const Text('Target Page')),
        body: Center(
          child: Consumer<PageNotifier>(
            builder: (context, notifier, child) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('Count: ${notifier.count}'),
                  const SizedBox(height: 16),
                  ElevatedButton(
                    onPressed: notifier.incrementCount,
                    child: const Text('Increment'),
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}

3. Push the Page to Navigator

Navigate to this page using the standard Navigator.push method. The provider will be created as part of the page's widget tree when it's pushed:

// From your home screen or another page
ElevatedButton(
  onPressed: () {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => const TargetPage()),
    );
  },
  child: const Text('Go to Target Page'),
)

Why This Works

  • Controlled Creation: The create callback runs only when the TargetPage widget is built—this happens precisely when the page is pushed onto the Navigator stack.
  • Auto-Disposal: When you pop the page (via Navigator.pop or the system back button), Flutter removes the entire TargetPage subtree from the widget tree. Since ChangeNotifierProvider is part of that subtree, it will automatically call dispose() on your PageNotifier, just like you'd expect from the provider package's default behavior.

Quick Heads-Up: Avoid ChangeNotifierProvider.value Here!

Don’t use the value constructor for this scenario—it’s designed for reusing existing notifier instances, not creating new ones tied to a page’s lifecycle. Stick with create to ensure proper creation and cleanup.

内容的提问来源于stack exchange,提问作者Prince

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:52:45