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';
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Can you help me to find if this list contains any item ? I mean servicesList.contains method.
1

Try this

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

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.