Reputation: 1
@override
Widget build(BuildContext context) {
return Card(
elevation: 6,
margin: EdgeInsets.all(20),
child: Padding(
padding: EdgeInsets.all(10),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: groupedTransactionValues.map((data) {
return Flexible(
fit: FlexFit.tight,
child: ChartBar(
data['day'],
data['amount'],
(data['amount'] as double) / totalSpending,
),
);
}).toList(),
),
),
);
}
}
Upvotes: 0
Views: 3320
Reputation: 31
This is actually because of you are trying to divide it with 0.So,just change the line of
(data['amount'] as double) / totalSpending
to this
totalSpending == 0 ? 0 : (data["amount"] as double) / totalSpending
If you have the problem says that "A RenderFlex overflowed by 36 pixels on the bottom."
Just change this:
child: Container(
height: 30,
child: Row(
children: groupedTransactionValues.map((data) {
return ChartBar(
data['day'],
data['amount'],
totalSpending == 0.0
? 0.0
: (data['amount'] as double) / totalSpending);
}).toList()),
)
to this:
child: Container(
height: 100,
child: Row(
children: groupedTransactionValues.map((data) {
return ChartBar(
data['day'],
data['amount'],
totalSpending == 0.0
? 0.0
: (data['amount'] as double) / totalSpending);
}).toList()),
)
Thats all, hope you can solve your problem!
Upvotes: 3
Reputation: 121
Change this:
(data['amount'] as double) / totalSpending
to this:
totalSpending == 0 ? 0 : (data["amount"] as double) / totalSpending
This error occurs when your transaction list is empty. If your transaction list is empty dart evaluates that expression as 0 / 0 which is NaN. When you pass this NaN to the ChartBar heightFactor: spendingPctOfTotal it throws that error because heightFactor is expecting a value bigger then 0 or null (NaN is different from null).
Upvotes: 12
Reputation: 8757
I am also taking that course and came to the same issue.
You need to pass the recent transactions into the chart like this:
List<Transaction> get _recentTransactions {
return _transactions.where((tx) => DateTime.now().difference(tx.date).inDays <= 7).toList();
}
and then use this in your build:
Chart(transactions: _recentTransactions);
Upvotes: 0