I have a list of transactions and i want to show total expense or balance at last index of listview..
I have taken expTotal for total Expense and i am initializing total before build method
its showing proper result when i have few datas but when i add more data and scroll listview, expTotal showing all time with some increments
and even if i place int expTotal=0 inside widget build..its showing the same.
here is my code
int expTotal=0;
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey.shade100,
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
Container(
height: 400,
color: Colors.transparent,
),
Expanded(
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: transactionList.length + 1,
itemBuilder: (context, index) {
if (index == transactionList.length) {
//ListTile for Total
return ListTile(
tileColor: Colors.grey,
leading: CircleAvatar(
backgroundColor: Colors.grey,
),
title: Center(child: Text('Total ')),
trailing: Text(expTotal.toString()),
);
}
final transactionModel = transactionList[index];
if(transactionModel.isExpense==true)
expTotal=expTotal+transactionModel.amount;
else
expTotal=expTotal-transactionModel.amount;
return ListTile(
leading: CircleAvatar(
child: Text(transactionModel.id),
),
title: Text(transactionModel.category),
trailing: Text(
transactionModel.amount.toString(),
style: TextStyle(
color: transactionModel.isExpense
? Colors.red
: Colors.blue),
),
);
}),
),
],
),
),
);
}
3
Answers
Instead of
you could write this to calculate it on the spot whenever you need it
Then you can leave out this part
and then it should work as expected while leaving the rest of the code the same
From the Flutter documentation:
Your code calculating expTotal expects the builder to pass through the list completely and only once, giving you an exact total, but in reality the builder is only building items that scroll onto the screen, so your expTotal is only triggered for a subset of the items that are displayed. Worse, if you scroll backwards and forwards, the visible elements would be counted in expTotal each time they are displayed.
It would be better to calculate the expTotal at the top of the build method, rather than inside the ListView.builder:
When you scroll through the list, the builder rebuilds widgets, and if you are performing calculations inside the builder, it can lead to incorrect results due to repeated calculations. You should calculate the total before building the list. Editing your provided code, you should have something similar to the below:
You could also move the computation from the
build
method toinitstate
.