DEV Community

01kg
01kg

Posted on

Flutter & Riverpod | How to Maintain the state of NavigationBar across tabs

I created a NavigationBar, 4 tabs, each one ref.watch() related provider. (Each provider basically fetches data from Database)

Riverpod caches provider state by default. But I found each time I tap a tab, the related provider would fetch again from DB.

According to Riverpod's doc:

If the UI stops using this provider, the cache will be destroyed. Then, if the UI ever uses the provider again, that a new network request will be made.

This indicates than the UI stops using the provider after another tab is tapped.

According to this Q&A answered by Remi Rousselet, the author of Riverpod:

You can use AutomaticKeepAliveClientMixin/IndexedStack if you want. Riverpod is unrelated to this

IndexedStack helped me.

Top comments (0)