使用个人理财应用程序,编码更精确。
getter应该返回一个日期列表,并为每个日期分配键。
当我运行代码时,应该打印出空列表,除非不是,并且当我添加新事务时,该错误会在模拟器中显示NoSuchMethodError: The methos '+' was called on Null. Receiver: null. Tried Calling: +(100)
我一点儿也不知道怎么了。
这是导致问题的类(class),
import 'package:demo_app/models/transaction.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
class Chart extends StatelessWidget {
final List<Transaction>recentTransactions;
Chart(this.recentTransactions) ;
List<Map<String,Object>> get groupedTransactionValues{
return List.generate(7, (index){
final weekDay = DateTime.now().subtract(Duration(days: index),);
double totalSum;
for(var i =0 ; i<recentTransactions.length;i++){
if(recentTransactions[i].date.day == weekDay.day && recentTransactions[i].date.month == weekDay.month && recentTransactions[i].date.year == weekDay.year){
totalSum+= recentTransactions[i].amount;
}
}
print(DateFormat.E().format(weekDay));
print(totalSum);
return{'day': DateFormat.E().format(weekDay) , 'amount': totalSum }; //this is a map
});
}
@override
Widget build(BuildContext context) {
print(groupedTransactionValues);
return Card(
elevation: 5,
margin: EdgeInsets.all(20),
child: Row(children: <Widget>[
],),
);
}
}
最佳答案
因为没有给totalSum
变量赋初始值,所以出现了错误。double totalSum = 0;
将解决该错误。
关于android - Flutter-在空上调用了 '+'方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62982248/