Home > front end >  Can I Change the data of a single particular item of a Listview.builder, Without losing the previous
Can I Change the data of a single particular item of a Listview.builder, Without losing the previous

Time:10-06

Let me Explain, Suppose I have a Listview.builder with 3 item and I Want to change the data of item no.2 without changing the other items data(1 and 3). And I have two data sources for updating on those 3 items according to button press.

Please Help me if you have any idea to solve this problem. Thank You

CodePudding user response:

Welcome @Rahul Choudhury!

ListView.builder has the property itemBuilder who accepts the index argument.
You can use that one!

final items = List<String>.generate(3, (i) => "Item $i");

const int specialIndex = 1;

return ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            if (specialIndex == index){
               //Use here your custom amazing widget
               return const ListTile( 
                  title: Text("Flutter is awesome"),
                  );
            }else {
               return ListTile(
                  title: Text(items[index]),
                  );
            }
          
          },
 );

Result

Obviously, I suggest you to refactor this code as you like, but I wanted to give you an idea ;)

  • Related