Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 颤振中的求和问题不起作用,如何在导航栏中显示总收入?_Firebase_Flutter_Visual Studio Code_Flutter Bottomnavigation - Fatal编程技术网

Firebase 颤振中的求和问题不起作用,如何在导航栏中显示总收入?

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

我想对集合名称(“januaryi”)下所有文档的字段(1月份的总收入)求和。我尝试了一个不起作用的函数。是否有方法调用函数并在底部导航栏中显示求和

扑动火源:

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中格式化代码,而不是发布图像