当前位置:网站首页>Use blocconsumer to build responsive components and monitor status at the same time
Use blocconsumer to build responsive components and monitor status at the same time
2022-07-07 22:00:00 【Ma Nong @ official account on the island with the same name】
Preface
In the last article Flutter Introduction and actual combat ( eighty-nine ): Use BlocListener Processing state changes , We used BlocListener Monitor the state changes and do some corresponding processing , At the same time BlocBuilder Build a responsive interface . Review the code , We will find that there are redundant parts , We need to BlocBuilder As BlocListener The child components , It's actually a little awkward , because BlocBuilder The built page should not be BlocListener The child components , It's a peer structure . Is there a better way to solve this problem , in fact ,flutter_bloc given BlocConsumer To solve this problem .
BlocConsumer Implementation mechanism
BlocConsumer Like BlocBuilder and BlocListener Aggregate component of , Support building responsive components while listening for state changes . At the same time, it also supports refreshing components according to conditions or listening callback in response to state changes .BlocConsumer The construction method of is as follows :
const BlocConsumer({
Key? key,
required this.builder,
required this.listener,
this.bloc,
this.buildWhen,
this.listenWhen,
}) : super(key: key);
The parameters are as follows :
builder: Responsive component construction method , andBlocBuilderIn the same .listener: Status change processing listening callback function , andBlocListenerThe definitions are the same .bloc: OptionalBlocState object , If not specified , Automatically from the currentBuildContextFind the corresponding type of status object in .buildWhen: Optional parameters , Status objects before and after receiving , You can return... According to the previous and subsequent status objectsboolvalue , iftrueWill refresh the component .listenWhen: Optional parameters , Status objects before and after receiving , You can return... According to the previous and subsequent status objectsboolvalue , iftrueWill calllistenerThe callback method .
Look again. BlocConsumer Of builder Method :
@override
Widget build(BuildContext context) {
if (widget.bloc == null) context.select<B, int>(identityHashCode);
return BlocBuilder<B, S>(
bloc: _bloc,
builder: widget.builder,
buildWhen: (previous, current) {
if (widget.listenWhen?.call(previous, current) ?? true) {
widget.listener(context, current);
}
return widget.buildWhen?.call(previous, current) ?? true;
},
);
}
You can see , The actual implementation is based on BlocBuilder Realization , And in the BlocBuilder Of builderWhen in , Will be based on listenWhen To determine whether to call listener The callback method , So that BlocBuilder and BlocListener The aggregation of .
BlocConsumer application
We modified the code of the previous article , Then take a look at the comparison of the two parts of the code , As shown in the figure below .

You can see , The number of lines of code has not changed much , But the hierarchy of the whole code will be clearer , This makes the code easier to maintain , The complete code has been uploaded to :BLoC Status management code .
summary
This article introduces BlocConsumer Implementation mechanism , And compared BlocBuilder and BlocListener Differences in separate implementations . Through comparison, we can see ,BlocConsumer This kind of aggregation BlocBuilder and BlocListener In this scenario, the code level is clearer , It's also easy to maintain . 
边栏推荐
- UVA 11080 – place the guards
- Leetcode SQL first day
- Index summary (assault version)
- Jerry's test box configuration channel [chapter]
- It's worth seeing. Interview sites and interview skills
- Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
- Meta force force meta universe system development fossage model
- Use br to back up tidb cluster data to azure blob storage
- Reinforcement learning - learning notes 8 | Q-learning
- The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function
猜你喜欢

L2: current situation, prospects and pain points of ZK Rollup

ByteDance Android interview, summary of knowledge points + analysis of interview questions

Virtual machine network configuration in VMWare

Why can't win11 display seconds? How to solve the problem that win11 time does not display seconds?

嵌入式开发:如何为项目选择合适的RTOS?

Win11如何解禁键盘?Win11解禁键盘的方法

Reinforcement learning - learning notes 9 | multi step TD target

The new version of onespin 360 DV has been released, refreshing the experience of FPGA formal verification function

TCP/IP 协议栈

The little money made by the program ape is a P!
随机推荐
201215-03-19 - cocos2dx memory management - specific explanation "recommended collection"
Nine degree 1201 - traversal of binary sort number - binary sort tree "suggestions collection"
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
MIT6.S081-Lab9 FS [2021Fall]
Demon daddy guide post - simple version
UVA 12230 – crossing rivers (probability) "suggested collection"
三元表达式、各生成式、匿名函数
Tupu digital twin coal mining system to create "hard power" of coal mining
Jerry's fast pairing does not support canceling pairing [article]
Ad domain group policy management
[C language] advanced pointer --- do you really understand pointer?
ByteDance senior engineer interview, easy to get started, fluent
How to make agile digital transformation strategy for manufacturing enterprises
UVA 11080 – place the guards
[colmap] sparse reconstruction is converted to mvsnet format input
operator
Build your own website (18)
双塔模型的最强出装,谷歌又开始玩起“老古董”了?
嵌入式开发:如何为项目选择合适的RTOS?
Main functions of OS, Sys and random Standard Libraries