flutter - 有没有办法让小部件等待它之前的小部件的结果
问题描述
我试图将 resultMsg 的值从 FutureBuilder 小部件传递给 Text 小部件,但它仍然给我变量的初始值,有没有办法在之前构建第一个小部件,获取变量更改(这是全局变量)并通过它?或任何其他方式来做即时消息发布我的代码以显示我要求的变量
child: ListView(
shrinkWrap: true,
children: <Widget>[
Container(
height: 400,
child: SafeArea(
child: FutureBuilder(
future: _getdeptemp(),
builder: (context, snapshot) {
if (snapshot.hasData) {
int x = (snapshot.data.length);
double xx = double.parse(x.toString());
//some process
resultMsg = ""+Sum ; //*********here is the value I want to assign variable to it**************
return
new charts.LineChart(
seriesList(dataa),
animate: false,
behaviors: [
new charts.ChartTitle('Session number',
behaviorPosition:
charts.BehaviorPosition.bottom,
//titleStyleSpec: charts.TextStyleSpec(fontSize: 14),
titleOutsideJustification: charts
.OutsideJustification.middleDrawArea),
new charts.ChartTitle(
'Right Pose Percentage, %',
behaviorPosition:
charts.BehaviorPosition.start,
//titleStyleSpec: chartsCommon.TextStyleSpec(fontSize: 11),
titleOutsideJustification: charts
.OutsideJustification.middleDrawArea)
],
);
} else {
return Center(child: CircularProgressIndicator());
}
}),
)),
Container(
child: new Text(
resultMsg,//////here is the variable ******
style: TextStyle(
color: Colors.black,
fontSize: 16,
),
)),
],
),
解决方案
如果你想在其他 Widget 中使用 FutureBuilder 的数据值,
那你把那个 Widget 插入到 FutureBuilder 里面怎么样?
在您的情况下,您需要通过 Column Widget 包装 LineChart 和 Container 小部件。
I just suggest but I don't know whether the error is because I don't have all source.
<As-is>
-ListView
--FutureBuilder
---LineChart
--Container
---Text
<To-be>
-ListView
--FutureBuilder
---Column
----LineChart
----Container
-----Text
or
-FutureBuilder
--ListView
---LineChart
---Container
----Text
这里是基于您的代码的建议代码,例如“To-be”。
ListView(
shrinkWrap: true,
children: <Widget>[
Container(
height: 400,
child: SafeArea(
child: FutureBuilder(
future: _getdeptemp(),
builder: (context, snapshot) {
if (snapshot.hasData) {
int x = (snapshot.data.length);
double xx = double.parse(x.toString());
//some process
resultMsg = ""+Sum ; //*********here is the value I want to assign variable to it**************
return Column(
children: [
new charts.LineChart(
seriesList(dataa),
animate: false,
behaviors: [
new charts.ChartTitle('Session number',
behaviorPosition:
charts.BehaviorPosition.bottom,
//titleStyleSpec: charts.TextStyleSpec(fontSize: 14),
titleOutsideJustification: charts
.OutsideJustification.middleDrawArea),
new charts.ChartTitle(
'Right Pose Percentage, %',
behaviorPosition:
charts.BehaviorPosition.start,
//titleStyleSpec: chartsCommon.TextStyleSpec(fontSize: 11),
titleOutsideJustification: charts
.OutsideJustification.middleDrawArea)
],
),
Container(
child: new Text(
resultMsg,//////here is the variable ******
style: TextStyle(
color: Colors.black,
fontSize: 16,
),
),
),
],
);
} else {
return Center(child: CircularProgressIndicator());
}
}),
)),
],
);
推荐阅读
- apache-flink - Flink 中事件处理的顺序和恢复
- php - 如果状态 ='1',我如何禁用按钮?(1=确认)
- couchbase - n1ql 主扫描性能
- mysql - 在 Laravel 中对分页变量进行排序
- javascript - 基于反应框架的网站是否在客户端的浏览器中隐藏代码?
- python - 如何使用 pydev 将 Eclipse 中的包展平?
- python - 如何组织应该在多行上的 cmd 参数?
- sorting - Api-平台儿童订单
- node.js - 如何使用 apollo 服务器从服务器设置响应标头?
- vert.x - Single 的 Vert-x RxJava Map 实现与 HttpResponse::body 参数不匹配