当前位置:网站首页>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
边栏推荐
- Resource packaging dependency tree
- Notes on xctf questions
- Non duplicate data values of two MySQL query tables
- Distributed message oriented middleware framework selection - Digital Architecture Design (7)
- Withholding agent
- What is augmented reality technology? Where can it be used?
- Give the method of instantiating the object to the new object
- AQS & reentrantlock implementation principle
- 基于ThinkPHP5版本TRC20-资金归集解决方案
- Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
猜你喜欢

EmuElec 编译总结

小程序报错:Cannot read property 'currentTarget' of undefined

Number of islands

机器学习(六)——贝叶斯分类器

To remember the composition ~ the pre order traversal of binary tree

108. Convert an ordered array into a binary search tree

Kettle实验

【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)

Brush classic topics

Applet error: should have URL attribute when using navigateto, redirectto or switchtab
随机推荐
L2-023 graph coloring problem (25 points) (graph traversal)
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
【SQL server速成之路】数据库的视图和游标
Flink SQL realizes the integration of stream and batch
js 原型链的深入
Valgrind and kcache grind use run analysis
To remember the composition ~ the pre order traversal of binary tree
Summary of solid problems
Arbre de dépendance de l'emballage des ressources
Give the method of instantiating the object to the new object
SQL used query statements
Enterprise wechat application authorization / silent login
Go language self-study series | golang structure pointer
Introduction to matlab
Node installation
Go language self-study series | initialization of golang structure
Single sign on SSO
How does kubernetes use harbor to pull private images
DJ music management software pioneer DJ rekordbox
RSA encryption and decryption signature verification