Ephemeral là gì
This doc introduces ứng dụng state, ephemeral state,and how you might manage each in a 6struyenky.vn app.
Bạn đang xem: Ephemeral là gì
In the broadest possible sense, the state of an tiện ích is everything thatexists in memory when the app is running. This includes the app’s assets,all the variables that the 6struyenky.vn framework keeps about the UI,animation state, textures, fonts, & so on. While this broadestpossible definition of state is valid, it’s not very useful forarchitecting an app.
First, you don’t even manage some state (like textures).The framework handles those for you. So a more useful definition ofstate is “whatever data you need in order khổng lồ rebuild your UI at anymoment in time”. Second, the state that you do manage yourself canbe separated into two conceptual types: ephemeral state and app state.
Ephemeral state
Ephemeral state (sometimes called UI state or local state)is the state you can neatly contain in a single widget.
This is, intentionally, a vague definition, so here are a few examples.
current progress of a complex animation current selected tab in a BottomNavigationBarOther parts of the widget tree seldom need lớn access this kind of state.There is no need khổng lồ serialize it, & it doesn’t change in complex ways.
In other words, there is no need khổng lồ use state management techniques(ScopedModel, Redux, etc.) on this kind of state.All you need is a StatefulWidget.
Below, you see how the currently selected thắng lợi in a bottom navigation bar isheld in the _index field of the _MyHomepageState class.In this example, _index is ephemeral state.
Xem thêm: Kiem Vu Giang Ho: Tin Tức, Clip, Video Hình Ảnh, Tin Mới Nhất Về Kiem Vu Giang Ho
class MyHomepage extends StatefulWidget const MyHomepage(super.key);
override State createState() => _MyHomepageState();class _MyHomepageState extends State int _index = 0;
override Widget build(BuildContext context) return BottomNavigationBar( currentIndex: _index, onTap: (newIndex) setState(() _index = newIndex; ); , // ... Items ... );
Here, using setState() và a field inside the StatefulWidget’s Stateclass is completely natural. No other part of your app needs to lớn access_index. The variable only changes inside the MyHomepage widget.And, if the user closes & restarts the app,you don’t mind that _index resets to lớn zero.
App state
State that is not ephemeral,that you want to nội dung across many parts of your app,and that you want to lớn keep between user sessions,is what we điện thoại tư vấn application state(sometimes also called shared state).
Examples of application state:
User preferences Login info Notifications in a social networking ứng dụng The shopping cart in an e-commerce tiện ích Read/unread state of articles in a news appFor managing phầm mềm state, you’ll want lớn research your options.Your choice depends on the complexity & nature of your app,your team’s previous experience, và many other aspects. Read on.
There is no clear-cut rule
To be clear, you can use State & setState() to lớn manage all ofthe state in your app. In fact, the 6struyenky.vn team does this in manysimple app samples (including the starter app that you get with every6struyenky.vn create).
It goes the other way, too. For example, you might decide that—inthe context of your particular app—the selected tab in a bottomnavigation bar is not ephemeral state. You might need lớn change itfrom outside the class, keep it between sessions, và so on.In that case, the _index variable is phầm mềm state.
There is no clear-cut, universal rule khổng lồ distinguishwhether 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 might need to be moved to ứng dụng state.
Xem thêm: Những Câu Thần Chú Cổ Xưa : Hiểu Được Thì Chuyện Khó Mấy Cũng Qua
For that reason, take the following diagram with a large grain of salt:

When asked about React’s setState versus Redux’s store, the tác giả of Redux,Dan Abramov, replied:
“The rule of thumb is: do whatever is less awkward.”
In summary, there are two conceptual types of state in any 6struyenky.vn app.Ephemeral state can be implemented using State & setState(),and is often local to a single widget. The rest is your phầm mềm state.Both types have their place in any 6struyenky.vn app, and the split betweenthe two depends on your own preference và the complexity of the app.