If you want to scope a ChangeNotifier to some routes using Provider? To do so, the easiest solution is to have one provider per route, such that instead of:
1 2 3 4 | Provider( builder: (_) => SomeValue(), child: MaterialApp(), ) |
You have:
1 2 3 4 5 6 7 8 9 | final value = SomeValue(); MaterialApp( routes: { '/foo': (_) => Provider.value(value: value, child: Foo()), '/bar': (_) => Provider.value(value: value, child: Bar()), '/cannot-access-provider': (_) => CannotAccessProvider(), } ) |
It is, on the other hand, not possible to have your model “automatically disposed”. provider is not able in such a situation to know that it is safe to dispose of the object.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.