当前位置:网站首页>Flutter之Provider共享数据的两种方式
Flutter之Provider共享数据的两种方式
2022-04-23 06:35:00 【可可鸭~】
Provider共享数据的两种方式
方式一:
Provider.of<CountViewModel>(context);
举例
import 'package:flutter/material.dart';
class CountViewModel extends ChangeNotifier{
int _count = 0;
//将变量暴露在外面
int get count=>_count;
addCount(){
_count++;
notifyListeners();//通知状态改变
}
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);//拿到provider
return Scaffold(
appBar:AppBar(
title: Text("计数器"),
),
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) 会导致调用的 context 页面范围的刷新。 Consumer 只刷新了 Consumer
的部分 建议尽量使用 Consumer 而不是 Provider.of(context) 获取顶层数据
方式二
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);//拿到provider
return Consumer(
builder: (context,CountViewModel counter,_){
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: (){
counter.addCount();
},
),
appBar: AppBar(
title: Text("Consumer数据共享"),
),
body: Text("${counter.count}"),
);
},
);
}
}
版权声明
本文为[可可鸭~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43547255/article/details/124298275
边栏推荐
- SAP自建表log功能开启
- Essays (updated from time to time)
- VBA調用SAP RFC實現數據讀取&寫入
- When using flash, the code ends automatically without an error, the connection cannot be maintained, and the URL cannot be accessed.
- Houdini > variable building roads, learning process notes
- upload-labs 靶场练习
- 内网渗透系列:内网隧道之icmp_tran
- 随笔(不定时更新)
- 国基北盛-openstack-容器云-环境搭建
- RAID0和RAID5的创建和模拟RAID5工作原理
猜你喜欢
BUUCTF [ACTF2020 新生赛]Include1
Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
Essays (updated from time to time)
BUUCTF MISC刷题
sentinel集成nacos动态更新数据原理
Redis -- why is the string length of string emstr the upper limit of 44 bytes?
Houdini fluid > > particle fluid export to unity note
[NLP notes] preliminary study on CRF principle
内网渗透系列:内网隧道之pingtunnel
SAP tr manual import system operation manual
随机推荐
Buctf MISC brossage
第五章 投资性房地产
Cloud computing skills competition -- Part 2 of openstack private cloud environment
nacos源码分析思路
SAP TR手动导入系统操作手册
Research on system and software security (3)
Link to some good tutorials or notes about network security and record them
学fpga(从verilog到hls)
SAP自建表log功能开启
Simplify exporting to SVG data files and all images in SVG folder
云计算技能大赛 -- openstack私有云环境 第一部分
[NLP notes] preliminary study on CRF principle
国基北盛-openstack-容器云-环境搭建
Internal network security attack and defense: a practical guide to penetration testing (8): Authority maintenance analysis and defense
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
Intranet security attack and defense: a practical guide to penetration testing (6): domain controller security
Three minutes to teach you to use Houdini fluid > > to solve particle fluid droplets
Chapter VII asset impairment
Towords Open World Object Detection
云计算技能大赛 -- openstack私有云环境 第二部分