当前位置:网站首页>Flutter life cycle
Flutter life cycle
2022-07-06 22:56:00 【Xiaobai is more than that】
One 、Flutter The two main Widget
StatelessWidget( Stateless component )
Stateless components are immutable
, This means that their properties cannot be changed , All values are final . It can be understood as transforming the external incoming data into the content displayed on the interface , It will only render once . For stateless component lifecycles, there are only build This process .
The construction method of stateless components is usually called in only three cases : The widget is inserted into the tree for the first time , The parent component of the widget changes its configuration , And what it depends on InheritedWidget When something changes .
StatefulWidget( Stateful component )
Stateful components may hold states in Widget Change in the life cycle
, Is to define interaction logic and business logic . It can be understood as having a dynamic and interactive content interface , It will render multiple times according to the change of data .
StatefulWidget The class itself is immutable , however State Class in Widget There is always in the life cycle .StatefulWidget Store its variable state in the createState Method to create State In the object , Or stored in the State In the subscribed object
.
Two 、StatefulWidget Life cycle
createState:
The function of StatefulWidget Created in State Methods , When StatefulWidget It will be executed immediately when it is created createState
.createState After the function is executed, it indicates that the current component is already in Widget In the tree , There is a very important attribute here mounted Be set to true.
initState:
The function of State Initialize call , Will only be called once
, therefore , Usually do some one-time operations in this callback , Such as implementation State Initial assignment of each variable 、 Subscribe to event notifications for subtrees 、 Interact with the server , After receiving server data, call setState To set up State.
didChangeDependencies:
The function is The component depends on State Is called when it changes
. here State by overall situation State
, For example, system language Locale Or application theme, etc ,Flutter The framework will inform widget Call this callback . After this method is called
, The state of the component changes to dirty, Call immediately build Method
.
build:
Mainly Returns the image to render Widget
, because build Will be called many times , Therefore, only returns can be made in this function Widget Related logic , Avoid state exceptions caused by multiple executions .
Use scenarios :
a. Calling initState() after .
b. Calling didChangeDependencies() after .
c. Calling setState() after .
d. Calling didUpdateWidget() after .
e. stay State After the object is removed from a location in the tree ( Would call deactivate) Reinsert again Enter into After going to other places in the tree .
reassemble:
The main Use during development , stay debug In mode , This function is called for each hot overload
, So in debug The stage can be increased during this period debug Code , To check for code problems . This callback stay release Mode will never be called
.
didUpdateWidget:
This function is mainly used in Component rebuild , For example, thermal overload
, The parent component build Under the circumstances , This method will only be called by the subcomponent , secondly After this method is called, it must call build Method .
deactivate:
After the node of the component is removed, it will be called
, If the component is removed, the node , then When not inserted into other nodes , Will continue to call dispose Permanently removed .
dispose:
Permanently remove components
, And release component resources . Call complete dispose after ,mounted Property is set to false, On behalf of The end of the component life cycle
.
It can be roughly divided into four stages :
(1) Initialization phase , Includes two lifecycle functions createState and initState;
(2) Component creation phase , Include didChangeDependencies and build;
(3) Trigger the component more than once build , This stage may be due to didChangeDependencies、 setState perhaps didUpdateWidget And the resulting component re build , It will be triggered many times during the operation of the component , This is also a key point in the optimization process ;
(4) The last stage is the component destruction stage ,deactivate and dispose.
This reference article is as follows :
interviewer : know Flutter Life cycle ? Come on next week !
边栏推荐
- Some suggestions for foreign lead2022 in the second half of the year
- Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
- dockermysql修改root账号密码并赋予权限
- Pytest unit test series [v1.0.0] [pytest execute unittest test case]
- Hard core observation 545 50 years ago, Apollo 15 made a feather landing experiment on the moon
- 如何实现文字动画效果
- hdu 5077 NAND(暴力打表)
- Case recommendation: An Qing works with partners to ensure that the "smart court" is more efficient
- docker中mysql开启日志的实现步骤
- On the problems of born charge and non analytical correction in phonon and heat transport calculations
猜你喜欢
Improving Multimodal Accuracy Through Modality Pre-training and Attention
Designed for decision tree, the National University of Singapore and Tsinghua University jointly proposed a fast and safe federal learning system
Export MySQL table data in pure mode
【全网首发】Redis系列3:高可用之主从架构的
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries
Thinkphp5 multi table associative query method join queries two database tables, and the query results are spliced and returned
MySQL中正则表达式(REGEXP)使用详解
Introduction to network basics
随机推荐
POJ 1258 Agri-Net
使用云服务器搭建代理
Volatile keyword
The statement that allows full table scanning does not seem to take effect set odps sql. allow. fullscan=true; I
Typescript get function parameter type
European Bioinformatics Institute 2021 highlights report released: nearly 1million proteins have been predicted by alphafold
C three ways to realize socket data reception
2014阿里巴巴web前实习生项目分析(1)
config:invalid signature 解决办法和问题排查详解
ThreadLocal详解
AdaViT——自适应选择计算结构的动态网络
Word2vec (skip gram and cbow) - pytorch
[unity] upgraded version · Excel data analysis, automatically create corresponding C classes, automatically create scriptableobject generation classes, and automatically serialize asset files
Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medi
Project duplicate template
MATLAB小技巧(27)灰色预测
ICLR 2022 | pre training language model based on anti self attention mechanism
欧洲生物信息研究所2021亮点报告发布:采用AlphaFold已预测出近1百万个蛋白质
Adavit -- dynamic network with adaptive selection of computing structure
How to use flexible arrays?