我有以下情况:FutureBuilder
方法中有一个build
:
FutureBuilder<List>(
future: NutritionDataManager().getDishes(),
initialData: List(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Dish> dishes = snapshot.data.map((dish) {
return Dish(dish.id, dish.icon, dish.name, dish.gram, dish.calories, dish.time);
}).toList();
countTotalCalories(dishes);
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return NutritionListItemWidget(
dish: snapshot.data[index],
);
},
childCount: snapshot.data.length,
),
);
} else {
return Container();
}
},
)
这个 future 将从数据库中获取数据,如果获取了数据(snapshot.hasData),它将调用一个方法,该方法将基于此数据进行计算:
countTotalCalories(List<Dish> dishes) {
int calSum = 0;
dishes.forEach((dish) => calSum += dish.calories);
_currentCalories = calSum;
}
然后在另一个Widget中使用此值(
_currentCalories
)。但是,当我运行我的应用程序时,_currentCalories
总是得到零值,但是当我按热重启时,一切正常。那么,这个问题怎么了,我该如何解决呢?提前致谢!UPD
这是我屏幕的整个代码:
class NutritionScreen extends StatefulWidget {
const NutritionScreen({Key key}) : super(key: key);
@override
_NutritionScreenState createState() => _NutritionScreenState();
}
class _NutritionScreenState extends State<NutritionScreen> {
int _currentCalories = 0;
@override
Widget build(BuildContext context) {
var date = "Aug 27";
return Scaffold(
backgroundColor: Color(AppColors.white),
floatingActionButton: FloatingActionButton(
backgroundColor: Color(AppColors.brandViolet),
onPressed: () => showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (context) => BottomNutritionModal(),
),
child: Icon(Icons.add),
),
body: SafeArea(
child: CustomScrollView(slivers: <Widget>[
SliverAppBar(
title: Padding(
padding: const EdgeInsets.only(right: 50.0),
child: Center(child: Text(date, style: TextStyle(color: Colors.black))),
),
backgroundColor: Colors.white,
floating: true,
automaticallyImplyLeading: true,
leading: IconButton(
icon: Icon(Icons.close, color: Colors.black),
onPressed: () => Navigator.pop(context, false),
),
elevation: 0,
),
SliverPersistentHeader(
delegate: _SliverPersistentHeaderDelegate(
child: PreferredSize(
preferredSize: Size.fromHeight(MediaQuery.of(context).size.height * 0.4),
child: LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
return Container(
color: Colors.white,
child: Column(
children: <Widget>[
RichText(
textAlign: TextAlign.center,
text: TextSpan(
style: TextStyle(color: Colors.black, fontSize: Dimensions.boldTextSize, fontWeight: FontWeight.bold),
children: <TextSpan>[
TextSpan(text: "You have consumed \n"),
TextSpan(text: "500 out of 1900 cal", style: TextStyle(color: Color(AppColors.brandViolet))),
],
),
),
Padding(
padding: const EdgeInsets.only(top: 10.0),
child: AnimatedCircularChart(
size: Size(constraints.maxHeight / 1.3, constraints.maxHeight / 1.3),
initialChartData: <CircularStackEntry>[
CircularStackEntry(
<CircularSegmentEntry>[
CircularSegmentEntry(33.33, Color(AppColors.brandViolet)),
CircularSegmentEntry(66.67, Color(AppColors.layoutBackgroundColor)),
],
),
],
chartType: CircularChartType.Radial,
edgeStyle: SegmentEdgeStyle.round,
percentageValues: true,
holeLabel: "$_currentCalories",
labelStyle: new TextStyle(
color: Colors.blueGrey[600],
fontWeight: FontWeight.bold,
fontSize: 35,
),
),
)
],
),
);
}),
),
),
),
FutureBuilder<List>(
future: NutritionDataManager().getDishes(),
initialData: List(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Dish> dishes = snapshot.data.map((dish) {
return Dish(dish.id, dish.icon, dish.name, dish.gram, dish.calories, dish.time);
}).toList();
countTotalCalories(dishes);
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return NutritionListItemWidget(
dish: snapshot.data[index],
);
},
childCount: snapshot.data.length,
),
);
} else {
return Container();
}
},
)
]),
),
// bottomNavigationBar: SafeArea(child: NutritionBottomInputWidget()),
);
}
countTotalCalories(List<Dish> dishes) {
int calSum = 0;
dishes.forEach((dish) => calSum += dish.calories);
setState(() {
_currentCalories = calSum;
});
}
}
最佳答案
使用以下代码可能会对您有所帮助。将_currentCalories放在setState方法中。
countTotalCalories(List<Dish> dishes) {
int calSum = 0;
dishes.forEach((dish) => calSum += dish.calories);
if (mounted){
setState(() {
_currentCalories = calSum;
});
}
}