0

I want to add an extra list that shows the total number of entries in ListView.builder().

My code is below, but it is not working as index not possible to compare to length.

return Scaffold(
      appBar: AppBar(title: Text('Exp Entries'),),
      body: ListView.builder(
          itemCount: noteprovider.expenseentries.length,
          itemBuilder: (context,index){

            if(index==noteprovider.expenseentries.length)
              return Text(noteprovider.expense_total.toString());
            else
             return Text(noteprovider.expenseentries[index].title);

          }),
    );

2
  • 2
    do you mean extra Item on ListView? Commented Oct 12, 2022 at 14:37
  • yes....but that extra item shows the total Commented Oct 12, 2022 at 16:09

1 Answer 1

3

You must add +1 to length on itemCount :

return Scaffold(
      appBar: AppBar(title: Text('Exp Entries'),),
      body: ListView.builder(
          itemCount: noteprovider.expenseentries.length + 1,
          itemBuilder: (context,index){

            if(index==noteprovider.expenseentries.length)
              return Text(noteprovider.expense_total.toString());
            else
             return Text(noteprovider.expenseentries[index].title);

          }),
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Ohh...so it was simple logic which I could not think about..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.