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 ?
CodePudding user response:
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';
CodePudding user response:
Try this
servicesList.removeWhere(
(item) => item['title'] == services[0].title);