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

How to remove Map from list in Dart | Flutter

I have a json list. from this i want to delete a particular item. Below is my code.

final List _vehicleList = [
    {
      'vehicleNumber': 'KL-14-V-5208',
      'vehicleType': 'Two Wheeler',
    },
    {
      'vehicleNumber': 'KL-13-H-8880',
      'vehicleType': 'Four Wheeler',
    },
    {
      'vehicleNumber': 'KL-14-M-6889',
      'vehicleType': 'Three Wheeler',
    },
  ];

This is my list. Here from this i want to delete the item based on vehicleNumber when i press a delete button. I’am using listview builder. When i print the list after the button press nothing happens
This is my UI Code.

                       return Column(
                            children: [
                              Padding(
                                padding: const EdgeInsets.all(12.0),
                                child: Text(
                                  _vehicleList[index]['vehicleNumber'],
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(12.0),
                                child: Text(
                                  _vehicleList[index]['vehicleType'],
                                ),
                              ),
                              GestureDetector(
                                onTap: () {
                                  print('Deleted');
                                  _vehicleList.removeAt(_vehicleList[index]);
                                  print(_vehicleList);
                                },
                                child: const Padding(
                                  padding: EdgeInsets.all(12.0),
                                  child: Icon(
                                    FontAwesomeIcons.timesCircle,
                                    color: Colors.redAccent,
                                  ),
                                ),
                              ),
                            ],
                          );

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 :

Try below code hope its help to you. just pass your index

_vehicleList.removeWhere((element) => element["vehicleNumber"] == 'KL-14-V-5208');
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