'Scoping Lifecycle of ChangeNotifier to just a specific number of screens

I have a ChangeNotifier Subclass that I am sharing between a number of screens like this:

final value = MyChangeNotifier();

MaterialApp(
  routes: {
    '/list': (_) => ChangeNotifierProvider.value(value: value, child: ListScreen()),
    '/detials': (_) => ChangeNotifierProvider.value(value: value, child: DetailsScreen()),
    '/other_screen': (_) => OtherScreen(),
  }
)

I want to dispose my ChangeNotifier once the user has left the last screen in this flow, and I want to create a new Instance of the shared ChangeNotifier once user re-enters the given flow.

Is it possible to do with Provider?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source