【发布时间】:2020-01-04 15:51:21
【问题描述】:
我的目标是计算该地点的平均值。但是,当我尝试计算时收到此错误:
类型'Future'不是类型'double'的子类型
预期是一个双精度类型,因为谁会收到这将是一个 RatingBar。
函数代码如下:
calculateEvaluations() async{
double average = 0;
var docs = await Firestore.instance.collection(widget.option).document(widget.document.documentID).collection("comments").getDocuments();
List<DocumentSnapshot> documents = docs.documents;
for(int i=0; i<documents.length; i++){
average = average + documents[i]["rating"].toDouble();
}
average = average / documents.length;
return average;
}
我将此返回设置为 RatingBar。
Container(
margin: EdgeInsets.only(left: 5.0),
child: RatingBarIndicator(
rating: calculateEvaluations(),
itemPadding: EdgeInsets.symmetric(horizontal: 2.0),
itemBuilder: (context, index) => Icon(
Icons.star,
color: Colors.yellow,
),
itemCount: 5,
itemSize: 18.0,
direction: Axis.horizontal,
),
),
【问题讨论】:
-
一旦将 calculateEvaluations 定义为异步函数,它就会变成 Future
,而不是 double。你应该 await 使用它作为一个 double,换句话说,你应该等待未来解决