How to Manage state between tabs in Flutter App

风格不统一 提交于 2019-12-22 19:30:16

问题


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.


回答1:


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).




回答2:


You may want to use architecture for managing and sharing states. There are many approaches to manage state:

  1. InheritedWidget to pass app state down the widget hierarchy
  2. Scoped model library to hold app state and notify Widgets of Updates
  3. Redux library to manage app state and update Widgets
  4. Business Logic Components (BLoC)

Check out this article. Check this GitHub repository with different architecture examples



来源:https://stackoverflow.com/questions/52247340/how-to-manage-state-between-tabs-in-flutter-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!