1

I created 3 list items to display using ListView.builder... but after coding the listView is displaying 1 item 3 times, then i added more items to the list making it a total of 5 items, then i discovered it started displaying each item 5 times before displaying the next item and i also noticed it is not scrollable

bellow is the code

import 'package:flutter/material.dart';
import './list_model.dart';

class Lists extends StatefulWidget {
  @override
  _ListsState createState() => _ListsState();
}

class _ListsState extends State<Lists> {
  List<ItemLists> itemsL = [
    ItemLists(
      title: 'Best Music of the Year',
      discription: 'Davido',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Album Cover design',
      discription: 'Brighter Press',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Vocalist',
      discription: 'Simi-Sola',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Danced',
      discription: 'Black Camaru',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Performance',
      discription: 'ShofeniWere',
      favorite: false,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Column(
        children: itemsL.map((items) {
      return Container(
        child: ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          itemCount: itemsL.length,
          itemBuilder: (context, index) {
            return Dismissible(
              key: ObjectKey(itemsL[index]),
              child: Card(
                  child: ListTile(
                leading: new IconButton(
                    icon: Icon(
                      Icons.star,
                      color: items.favorite ? Colors.green : Colors.grey,
                    ),
                    tooltip: 'Add to Favorite',
                    onPressed: () {
                      setState(() {
                        items.favorite = !items.favorite;
                      });
                    }),
                title: Text('${items.title}'),
                subtitle: Text('${items.discription}'),
                trailing: IconButton(
                    icon: Icon(Icons.calendar_today), onPressed: null),
              )),
            );
          },
        ),
      );
    }).toList());
  }
}

below is my list model

class ItemLists {
  String title;
  String discription;
  bool favorite;

  ItemLists({this.title, this.discription, this.favorite});
}

below is the screenshot

enter image description here

1
  • why are you mapping a list inside a column... you're literally creating a full list out of each element. Commented Apr 15, 2021 at 19:21

2 Answers 2

1

It is because your itemsL is mapping its each data to a ListView.Builder. ListView.Builder has its own way of iterating through a list... So the itemsL.map((items) is not necessary... Instead you should do it in this way

Where you use the index of the ListView.builder

ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          itemCount: itemsL.length,
          itemBuilder: (context, index) {
            return Dismissible(
              key: ObjectKey(itemsL[index]),
              child: Card(
                  child: ListTile(
                leading: new IconButton(
                    icon: Icon(
                      Icons.star,
                      // using the index of listViewBuilder
                      color: itemsL[index].favorite ? Colors.green : Colors.grey,
                    ),
                    tooltip: 'Add to Favorite',
                    onPressed: () {
                      setState(() {
                        items.favorite = !items.favorite;
                      });
                    }),
                //same over here
                title: Text('${itemsL[index].title}'),
                subtitle: Text('${itemsL[index].discription}'),
                trailing: IconButton(
                    icon: Icon(Icons.calendar_today), onPressed: null),
              )),
            );
          },
        ),
      );
Sign up to request clarification or add additional context in comments.

1 Comment

it is correct but after using the code i got an error from my setState on the items.favorite... but got the idea to fix that issue from the answer bellow... but your answer is still correct
0

You were using map function to make multiple ListView.builders, this is the solution:

    class Lists extends StatefulWidget {
  @override
  _ListsState createState() => _ListsState();
}

class _ListsState extends State<Lists> {
  List<ItemLists> items = [
    ItemLists(
      title: 'Best Music of the Year',
      discription: 'Davido',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Album Cover design',
      discription: 'Brighter Press',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Vocalist',
      discription: 'Simi-Sola',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Danced',
      discription: 'Black Camaru',
      favorite: false,
    ),
    ItemLists(
      title: 'Best Performance',
      discription: 'ShofeniWere',
      favorite: false,
    ),
    
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Dismissible(
            key: ObjectKey(items[index]),
            child: Card(
                child: ListTile(
              leading: new IconButton(
                  icon: Icon(
                    Icons.star,
                    color: items[index].favorite ? Colors.green : Colors.grey,
                  ),
                  tooltip: 'Add to Favorite',
                  onPressed: () {
                    setState(() {
                      items[index].favorite = !items[index].favorite;
                    });
                  }),
              title: Text('${items[index].title}'),
              subtitle: Text('${items[index].discription}'),
              trailing:
                  IconButton(icon: Icon(Icons.calendar_today), onPressed: null),
            )),
          );
        },
      ),
    );
  }
}

Comments

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.