0

Hello everyone I have a list of maps. And I want to use removeWhere() method with this.

My List is here:

List<Map<String?, dynamic>> servicesList= [
                    {
                      'title': services[0].title,
                      'price': services[0].price,
                    },
                    {
                      'title': services[1].title,
                      'price': services[1].price,
                    },
                    {
                      'title': services[2].title,
                      'price': services[2].price,
                    },
                    

I am using like this because with another button, I am adding some maps inside this list.

But whenever I want to removeWhere() method I failed.

 servicesList.removeWhere(
                    (item) =>
                        item ==
                        ({
                          'title': services[0].title,
                          'price': services[0].price,
                        }),
                  );
print(servicesList);

It doesn't work with it. How do I handle with this method ?

2 Answers 2

1

use mapEquals instead of == since == technically is comparing pointers not values, like

List<Map<String?, dynamic>> servicesList= [
                    {
                      'title': 'title0',
                      'price': 'price0',
                    },
                    {
                      'title': 'title1',
                      'price': 'price1',
                    },
                    {
                      'title': 'title2',
                      'price': 'price2',
                    },];
                    
  servicesList.removeWhere(
                    (item) =>
                     mapEquals(item,
                        ({
                          'title': 'title0',
                          'price': 'price0',
                        })),
                  );
print(servicesList);

you also need to import:

import 'package:flutter/foundation.dart';
1
  • Thank you. Can you help me to find if this list contains any item ? I mean servicesList.contains method. Mar 21, 2022 at 16:21
1

Try this

 servicesList.removeWhere(
                    (item) => item['title'] == services[0].title);
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.