我试图将 resultMsg 的值从 FutureBuilder 小部件传递给 Text 小部件,但它仍然给我变量的初始值,有没有办法在之前构建第一个小部件,获取变量更改(这是全局变量)并通过它? 或任何其他方式 IM 发布我的代码以显示我要求的变量
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,
),
)),
],
),
答案 0 :(得分:0)
如果你想在其他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());
}
}),
)),
],
);