【发布时间】:2021-12-02 21:07:52
【问题描述】:
我收到三个错误:
- 参数类型“对象?”不能分配给参数类型“num”。行 - 返回总和 + item['amount'];
- 没有为“Set”类型定义运算符“/”。 尝试定义运算符'/'。行 - """ {data['amount'] as double} / totalSpending),"""
- 参数类型“对象?”不能分配给参数类型'double'.Line - """data['amount'],"""
我尝试使用双重解析,但它只解决了第三个错误,然后给出了错误: .参数类型“对象?”不能赋值给参数类型'string'
前两个我无法完全解决。
import 'package:expense_app/Widget/chart_bar.dart';
import 'package:expense_app/models/transaction.dart';
import 'package:flutter/material.dart';
import '../models/transaction.dart';
import 'package:intl/intl.dart';
import '../Widget/chart_bar.dart';
class Chart extends StatelessWidget {
//const Chart({ Key? key }) : super(key: key);
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),
);
var totalSum = 0.0; //using double instead of var to initiliaze it
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)
.substring(0, 1), //shortcut or giving us weekdays
'amount': totalSum,
};
}).reversed.toList(); //This will give us a list with 7 elements
}
double get totalSpending {
return groupedTransactionValues.fold(0.0, (sum, item) {
return sum + item['amount'];
});
}
@override
Widget build(BuildContext context) {
print(groupedTransactionValues);
return Card(
elevation: 6,
margin: EdgeInsets.all(20),
child: Container(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: groupedTransactionValues.map((data) {
return Flexible(
fit: FlexFit
.tight, //the child can grow and take extra space from others making the look clumsy with larger values
child: Chartbar(
data['day'].toString(),
data['amount'],
totalSpending == 0.0
? 0.0
: {data['amount'] as double} / totalSpending),
);
}).toList(),
),
),
);
}
}
【问题讨论】:
-
你能告诉我们每一行的错误吗?如此有限的信息很难为您提供帮助。
-
嘿,我已经添加了出现这些错误的行