Firebase 颤振中的求和问题不起作用,如何在导航栏中显示总收入?
我想对集合名称(“januaryi”)下所有文档的字段(1月份的总收入)求和。我尝试了一个不起作用的函数。是否有方法调用函数并在底部导航栏中显示求和 扑动火源:Firebase 颤振中的求和问题不起作用,如何在导航栏中显示总收入?,firebase,flutter,visual-studio-code,flutter-bottomnavigation,Firebase,Flutter,Visual Studio Code,Flutter Bottomnavigation,我想对集合名称(“januaryi”)下所有文档的字段(1月份的总收入)求和。我尝试了一个不起作用的函数。是否有方法调用函数并在底部导航栏中显示求和 扑动火源: class _JanuaryState extends State<January>{ totalmoney() async { int total = 0; var snapshot = await Firestore.instance.collection("Januaryi&q
class _JanuaryState extends State<January>{
totalmoney() async {
int total = 0;
var snapshot =
await Firestore.instance.collection("Januaryi").getDocuments();
if (snapshot == null){
return 0;
}
snapshot.documents.foreach((doc) {
total = total + int.parse(doc.data['income']);
});
return total;
}
}
bottomNavigationBar: Container(
child: ListTile(
title: Text('Total'),
subtitle: Text("${totalmoney()}"),
)),
);
功能:
class _JanuaryState extends State<January>{
totalmoney() async {
int total = 0;
var snapshot =
await Firestore.instance.collection("Januaryi").getDocuments();
if (snapshot == null){
return 0;
}
snapshot.documents.foreach((doc) {
total = total + int.parse(doc.data['income']);
});
return total;
}
}
bottomNavigationBar: Container(
child: ListTile(
title: Text('Total'),
subtitle: Text("${totalmoney()}"),
)),
);
显示总收入中的错误:
class _JanuaryState extends State<January>{
totalmoney() async {
int total = 0;
var snapshot =
await Firestore.instance.collection("Januaryi").getDocuments();
if (snapshot == null){
return 0;
}
snapshot.documents.foreach((doc) {
total = total + int.parse(doc.data['income']);
});
return total;
}
}
bottomNavigationBar: Container(
child: ListTile(
title: Text('Total'),
subtitle: Text("${totalmoney()}"),
)),
);
像您尝试的那样更改构建函数内部的值不是一种好的做法。 您只需在
totalmoney()
函数中设置一个totalIncome
变量,如下所示:
totalmoney() async{
int total = 0;
var snapshot = await Firestore.instance.collection("Januaryi").getDocuments();
setState((){
if(snapshot == null)
totalIncome = 0
snapshot.documents.forEach((doc){
total+=int.parse(doc.data['income']);
});
totalIncome = total;
});
}
然后在initState()方法中调用此方法。
将${totalmoney()}
替换为新变量$totalIncome
。别忘了在State类的顶部初始化它
另外,尝试在StackOverflow中格式化代码,而不是发布图像。像您尝试的那样更改构建函数中的值不是一个好的做法。
您只需在totalmoney()
函数中设置一个totalIncome
变量,如下所示:
totalmoney() async{
int total = 0;
var snapshot = await Firestore.instance.collection("Januaryi").getDocuments();
setState((){
if(snapshot == null)
totalIncome = 0
snapshot.documents.forEach((doc){
total+=int.parse(doc.data['income']);
});
totalIncome = total;
});
}
然后在initState()方法中调用此方法。
将${totalmoney()}
替换为新变量$totalIncome
。别忘了在State类的顶部初始化它
另外,尝试在StackOverflow中格式化代码,而不是发布图像