当前位置:网站首页>Flutter Builder & FutureBuilder组件
Flutter Builder & FutureBuilder组件
2022-06-25 03:53:00 【xiangxiongfly915】
Flutter Builder & FutureBuilder组件
Builder组件
Builder组件起闭包作用,可以限制期使用范围。
问题
class BuilderPage extends StatelessWidget {
const BuilderPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Builder组件"),
),
body: Center(
child: RaisedButton(
color: Colors.red,
textColor: Colors.white,
onPressed: () {
showSnackBar(context);
},
child: const Text('show SnackBar'),
),
),
);
}
showSnackBar(BuildContext context) {
const snackBar = SnackBar(content: Text('老孟'));
Scaffold.of(context).showSnackBar(snackBar);
}
}
点击按钮后,会提示异常信息:Scaffold.of() called with a context that does not contain a Scaffold.。
这是因为你传入的BuildContext是当前parent Widget的,不是Scaffold的,所以会报错。
解决
可以借助Builder组件解决。
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Builder组件"),
),
body: Builder(
builder: (context) {
return Center(
child: RaisedButton(
color: Colors.red,
textColor: Colors.white,
onPressed: () {
showSnackBar(context);
},
child: const Text('show SnackBar'),
),
);
}
),
);
}
FutureBuilder组件
展示异步任务状态,当有一个Future异步任务需要展示给用户时,可以使用FutureBuilder组件来实现。

final Future<String> _futureSuccess = Future.delayed(const Duration(seconds: 3), () {
return "成功了";
});
final Future<dynamic> _futureError = Future.delayed(const Duration(seconds: 3), () {
return Future.error("失败了");
});
String _msg = "加载中";
String _msg2 = "加载中";
FutureBuilder(
future: _futureSuccess,
builder: (context, snapshot) {
late Widget widget;
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
widget = const Icon(Icons.error, color: Colors.red, size: 50);
_msg = snapshot.error.toString();
} else {
widget = const Icon(Icons.check_circle, color: Colors.green, size: 50);
_msg = snapshot.data.toString();
}
} else {
widget = const CircularProgressIndicator();
_msg = "加载中";
}
return Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
widget,
Text(_msg),
],
),
),
);
},
),
FutureBuilder(
future: _futureError,
builder: (context, snapshot) {
late Widget widget;
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
widget = const Icon(Icons.error, color: Colors.red, size: 50);
_msg2 = snapshot.error.toString();
} else {
widget = const Icon(Icons.check_circle, color: Colors.green, size: 50);
_msg2 = snapshot.data.toString();
}
} else {
widget = const CircularProgressIndicator();
_msg2 = "加载中";
}
return Container(
height: 100,
width: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: const BorderRadius.all(Radius.circular(10)),
),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
widget,
Text(_msg2),
],
),
),
);
},
)
边栏推荐
- 程序猿职业发展9项必备软技能
- Crawler crawls Sina Weibo data
- Solution to the problem that Linux crontab timed operation Oracle does not execute (crontab environment variable problem)
- Hello CTP (II) -- Introduction to CTP
- Google founder brin's second marriage broke up: it was revealed that he had filed for divorce from his Chinese wife in January, and his current fortune is $631.4 billion
- MySQL根据表前缀批量修改、删除表
- Development of trading system (VI) -- HFT high frequency trading
- Hello CTP (I) - basic knowledge of futures
- 学习码 滚动码 固定码 有什么区别重码数,编码容量滚动码的原理
- How to use crawlers to capture bullet screen and comment data of station B?
猜你喜欢

La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox

练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
![[team learning] SQL programming language notes - task04](/img/3e/c75f5b4610c6b6700567fe75cd2339.png)
[team learning] SQL programming language notes - task04

你真的需要自动化测试吗?

【Rust投稿】捋捋 Rust 中的 impl Trait 和 dyn Trait

Development of trading system (VIII) -- Construction of low delay network

How far is the memory computing integrated chip from popularization? Listen to what practitioners say | collision school x post friction intelligence

完美洗牌问题
佐喃社区

zabbix的安装避坑指南
随机推荐
AI quantitative transaction (II) -- tushare financial data framework
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
About PLSQL error initialization failure
教你如何在winpe里安装win11系统
2022-06-21-Flink-49(一. SQL手册)
Work assessment of pharmacotherapeutics of Jilin University in March of the 22nd spring -00064
老叶的祝福
Google founder brin's second marriage broke up: it was revealed that he had filed for divorce from his Chinese wife in January, and his current fortune is $631.4 billion
Serious PHP defects can lead to rce attacks on QNAP NAS devices
The art of writing simple code
What is the difference between learning code, rolling code and fixed code? The number of repeated codes, coding capacity and the principle of rolling code
练习:仿真模拟福彩双色球——中500w巨奖到底有多难?跑跑代码就晓得了。
AI quantitative transaction (I) -- Introduction to quantitative transaction
Jilin University 22 spring March "technical economics" assignment assessment-00073
AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
数学分析_笔记_第3章:极限
Why can banana be a random number generator? Because it is the "king of radiation" in the fruit industry
Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
Create SQLite table with shell script and add SQL statement -- General
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00031