当前位置:网站首页>BlocProvider 为什么感觉和 Provider 很相似?
BlocProvider 为什么感觉和 Provider 很相似?
2022-07-01 21:38:00 【InfoQ】
前言
SimpleBlocProviderBlocBlocProviderProvider 的模仿者?
BlocProviderclass BlocCounterWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => CounterCubit(),
child: BlocCounterPage(),
);
}
}
class BlocCounterPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Bloc 计数器'),
),
body: Center(
child: BlocBuilder<CounterCubit, int>(
builder: (context, count) => Text(
'$count',
style: TextStyle(
fontSize: 32,
color: Colors.blue,
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
context.read<CounterCubit>().increment();
},
tooltip: '点击增加',
child: Icon(Icons.add),
),
);
}
}
ProviderWidget build(BuildContext context) {
return BlocProvider(
create: (_) => CounterCubit(),
child: BlocCounterPage(),
);
}
ProviderWidget build(BuildContext context) {
return Provider(
create: (_) => CounterCubit(),
child: BlocCounterPage(),
);
}
FloatingActionButtoncontext.readProviderProviderlibrary flutter_bloc;
export 'package:bloc/bloc.dart';
export 'package:provider/provider.dart'
show ProviderNotFoundException, ReadContext, SelectContext, WatchContext;
ProviderblocProvider

与 Provider 的异同
BlocProviderProviderProviderBlocProviderProvidervaluefinal counter = CounterCubit();
//...
BlocProvider.value(
value: counter,
child: SomeWidget(),
);
context.readcontext.watchfinal isPositive = context.select((CounterBloc b) => b.state >= 0);
MultiBlocProviderMultiBlocProvider(
providers: [
BlocProvider<BlocA>(
create: (BuildContext context) => BlocA(),
),
BlocProvider<BlocB>(
create: (BuildContext context) => BlocB(),
),
BlocProvider<BlocC>(
create: (BuildContext context) => BlocC(),
),
],
child: ChildA(),
)
ProviderBlocProviderBlocProviderProviderChangeNotifiernotifyListenersBlocProviderSimpleBlocProviderStream.listenstatic VoidCallback _startListening(
InheritedContext<BlocBase?> e,
BlocBase value,
) {
final subscription = value.stream.listen(
(dynamic _) => e.markNeedsNotifyDependents(),
);
return subscription.cancel;
}
ProviderBlocProviderStream.listenChangeNotiferProviderProviderBlocProvidernotifyListeners总结
BlocProviderProviderBlocProviderProvider
边栏推荐
- Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
- 杰理之关于长按开机检测抬起问题【篇】
- 如果浏览器被意外关闭,react怎么缓存用户填写的表单?
- 十三届蓝桥杯B组国赛
- Oracle deadlock test
- 分离字符串中的字母和数字并使得字母在前数组在后
- 新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码
- Fundamentals - IO intensive computing and CPU intensive computing
- 图片拼图微信小程序源码_支持多模板制作和流量主
- PCB plug hole technology~
猜你喜欢

Penetration tools - trustedsec's penetration testing framework (PTF)

函数基本学习之一
![[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)](/img/bf/524e78473625a31c024783ccec8d46.png)
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)

杰理之、产线装配环节【篇】

新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码

PMP证书真的有用吗?

东哥套现,大佬隐退?

BC35&BC95 ONENET MQTT(旧)

K-means based user portrait clustering model

十三届蓝桥杯B组国赛
随机推荐
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
Test cancellation 1
leetcode刷题:栈与队列01(用栈实现队列)
旁路由设置的正确方式
JS how to get a list of elements in a collection object
Case of camera opening by tour
王者战力查询改名工具箱小程序源码-带流量主激励广告
在技术升级中迎合消费者需求,安吉尔净水器“价值战”的竞争之道
Halcon知识:三维重构的一个尝试
2022年高处安装、维护、拆除考题模拟考试平台操作
【STM32】STM32CubeMX教程二–基本使用(新建工程点亮LED灯)
PCB线路板塞孔工艺的那些事儿~
深度学习 常见的损失函数
News classification based on LSTM model
Significance and measures of security encryption of industrial control equipment
K-means based user portrait clustering model
如果浏览器被意外关闭,react怎么缓存用户填写的表单?
辅音和声母的区别?(声母与辅音的区别)
MQ学习笔记
朋友圈社区程序源码分享