When would you use App state or Ephemeral state over another?

Technology CommunityCategory: FlutterWhen would you use App state or Ephemeral state over another?
VietMX Staff asked 3 years ago
  • Ephemeral state can be implemented using State and setState(), and is often local to a single widget while rest state is the app state.
  • To be clear, you can use State and setState() to manage all of the state in your app.
  • It goes the other way, too. For example, you might decide that—in the context of your particular app—the selected tab in a bottom navigation bar is not ephemeral state. You might need to change it from outside the class, keep it between sessions, and so on. In that case, the _index variable is app state.
  • Both types have their place in any Flutter app, and the split between the two depends on your own preference and the complexity of the app.
  • There is no clear-cut, universal rule to distinguish whether a particular variable is ephemeral or app state. Sometimes, you’ll have to refactor one into another. For example, you’ll start with some clearly ephemeral state, but as your application grows in features, it will need to be moved to app state.