Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Title name of my new card changes all the other cards' name to the new title name

I have a listview which displays cards on but whenever I add a new card, the title of all the cards is updated to the new title but I need just the current added one to change

final taskName = TextEditingController();

 TextField(
                  controller: taskName,
                  decoration: const InputDecoration(hintText: 'Task Name'),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      counter++;
                      Navigator.pop(context);
                    },
                    child: const Text(
                      'Add',
                      style: TextStyle(fontSize: 15),
                    ),
                  ),

Expanded(
            child: ListView.builder(
                itemCount: counter,
                itemBuilder: (BuildContext context, index) {
                  return TodoContainer(
                    cardTitle: taskName.text.toString(),
                  );
                }),
          )

ScreenShot to show how my cards look like after adding a new card with name of TODO

ScreenShot to show how my cards look like after adding a new card with name of TODO

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are using a single TextEditingController value to populate the ListView, therefore listview children is getting update value instead of creating new item.

final taskName = TextEditingController();

What you can do, create a list of String and feed it on children.

  List<String> items = []; // on state class

 .... 
 // add data 
  onPressed: () {
    items.add(taskName.text.toString()); 
     ....
      },

......

/// populate listView
 return TodoContainer(
         cardTitle: items[index],
             );

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading