DEV Community

01kg
01kg

Posted on

1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay