当前位置:网站首页>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
边栏推荐
- [Luke V0] verification environment 2 - Verification Environment components
- Arbre de dépendance de l'emballage des ressources
- 112. 路径总和
- 爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
- AQS & reentrantlock implementation principle
- LeetCode396. Rotate array
- Bk3633 specification
- Find the sum of simple types of matrices
- MySQL小练习(仅适合初学者,非初学者勿进)
- LGB, XGB, cat, k-fold cross validation
猜你喜欢
[Luke V0] verification environment 2 - Verification Environment components
《信息系统项目管理师总结》第八章 项目干系人管理
What is augmented reality technology? Where can it be used?
GoLand debug go use - white record
Principle of synchronized implementation
Redis Desktop Manager for Mac
Share the office and improve the settled experience
The crawler returns null when parsing with XPath. The reason why the crawler cannot get the corresponding element and the solution
The K neighbors of each sample are obtained by packet switching
Cross domain configuration error: when allowcredentials is true, allowedorigins cannot contain the special value "*“
随机推荐
Flink reads MySQL and PgSQL at the same time, and the program will get stuck without logs
Correct method of calculating inference time of neural network
MySQL小练习(仅适合初学者,非初学者勿进)
AQS & reentrantlock implementation principle
Rembg split mask
Find the sum of simple types of matrices
Bk3633 specification
501. Mode in binary search tree
【SQL server速成之路】数据库的视图和游标
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
The K neighbors of each sample are obtained by packet switching
npm ERR! network
Data visualization: use Excel to make radar chart
LeetCode396. Rotate array
Little girl walking
Notes on xctf questions
Detailed explanation of delete, truncate and drop principles in MySQL database
Harbor enterprise image management system
[boutique] using dynamic agent to realize unified transaction management II
Withholding agent