当前位置:网站首页>Two ways for flutter providers to share data
Two ways for flutter providers to share data
2022-04-23 09:17:00 【Ke Ke duck~】
Provider Two ways to share data
Mode one :
Provider.of<CountViewModel>(context);
give an example
import 'package:flutter/material.dart';
class CountViewModel extends ChangeNotifier{
int _count = 0;
// Expose variables
int get count=>_count;
addCount(){
_count++;
notifyListeners();// Notification status change
}
deCount(){
_count--;
notifyListeners();
}
}
import 'package:flutter/material.dart';
import 'package:navigatoritem/page/CountViewModel.dart';
import 'package:provider/provider.dart';
class B extends StatefulWidget{
_BState createState() =>_BState();
}
class _BState extends State<B>{
@override
Widget build(BuildContext context){
final counter = Provider.of<CountViewModel>(context);// Get provider
return Scaffold(
appBar:AppBar(
title: Text(" Counter "),
),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
Provider.of<CountViewModel>(context,listen: false).addCount();
},
),
body: Container(
child: Center(
child: Text("${counter.count}"),
),
),
);
}
}
Provider.of(context) Will result in a call context Page range refresh . Consumer Only refreshed Consumer
Part of It is recommended to use Consumer instead of Provider.of(context) Get top-level data
Mode two
import 'package:flutter/material.dart';
import 'package:navigatoritem/page/CountViewModel.dart';
import 'package:provider/provider.dart';
class B extends StatefulWidget{
_BState createState() =>_BState();
}
class _BState extends State<B>{
@override
Widget build(BuildContext context){
final counter = Provider.of<CountViewModel>(context);// Get provider
return Consumer(
builder: (context,CountViewModel counter,_){
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
counter.addCount();
},
),
appBar: AppBar(
title: Text("Consumer Data sharing "),
),
body: Text("${counter.count}"),
);
},
);
}
}
版权声明
本文为[Ke Ke duck~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230634392472.html
边栏推荐
- Thread scheduling (priority)
- Illegal character in scheme name at index 0:
- Valgrind et kcachegrind utilisent l'analyse d'exécution
- 112. Path sum
- I don't understand time, timestamp and time zone. Look at this article
- npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
- 《数字电子技术基础》3.1 门电路概述、3.2 半导体二极管门电路
- [in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
- Flink同时读取mysql与pgsql程序会卡住且没有日志
- The most concerned occupations after 00: civil servants ranked second. What was the first?
猜你喜欢
调包求得每个样本的k个邻居
MySQL小練習(僅適合初學者,非初學者勿進)
3、 6 [Verilog HDL] gate level modeling of basic knowledge
To remember the composition ~ the pre order traversal of binary tree
Summary of wrong questions 1
kettle实验
653. 两数之和 IV - 输入 BST
ATSS(CVPR2020)
First principle mind map
DJ music management software pioneer DJ rekordbox
随机推荐
Image processing in opencv -- Introduction to contour + contour features
Multi view depth estimation by fusing single view depth probability with multi view geometry
Production practice elk
Get trustedinstaller permission
Thread scheduling (priority)
NPM installation yarn
Kettle实验 转换案例
Failed to download esp32 program, prompting timeout
108. Convert an ordered array into a binary search tree
SAP 101K 411K 库存变化
考研线性代数常见概念、问题总结
JS prototype chain
501. 二叉搜索树中的众数
Error: cannot find or load main class
web页面如何渲染
Applet in wechat and app get current ()
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
112. Path sum
三、6【Verilog HDL】基础知识之门级建模