Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Manage state between tabs in Flutter App

I am using TabBar in my application where I need to get Input in first tab and show
output based on some calculation on other tab.

I am able to get input in first tab but when I click on second tab, my state is lost. I am not sure if I have to make it a Stateful widget at root level.

I am relatively new in Mobile App. Can someone help me out here?

EDIT:

Now I am able to achieve what I said above, but when I am trying to access the list of items from first tab, it throws null exception in second tab?

I solved the above problem using AutomaticKeepAliveClientMixin as described in the answer below.

like image 855
Manas Singhal Avatar asked Mar 05 '23 11:03

Manas Singhal


1 Answers

I think what you may be after is AutomaticKeepAliveClientMixin. I always add that to my screen widgets within TabBarView. This way when a user enters data into fields in one tab and navigates to other tabs and back the data is still there as you would expect. Then when the user performs an action like "save" I deal with state management (BLoC etc).

like image 95
Jason Scott Avatar answered Mar 16 '23 13:03

Jason Scott