Flutter How to search a spesific map inside of list?

Issue

I have 3 selectable item on my UI.
I created these with ListViewBuilder. My goal is upload the selectedItem‘s to Firebase. Anyway, in example;

List<Map<String?, dynamic>> selectedItems = List.empty(growable: true);

List is gonna be like this right before uploading Firebase:

List selectedItems = [
      {
        'title': selectedItem[index].title,
        'price': selectedItem[index].price,
      },
    ];

This has to be my selectedItems list.

And I upload this list as Map like this:

 Map<String, dynamic> updatedData = {'items': selectedItems};

This has to be like this in order to Firebase design.

First of all when the user has clicked to item I want to check selectedItems list.
Here is my code but it doesn’t work at all:

if (selectedServices.contains(
                           [{
                           'title': selectedItem[index].title,
                           'price': selectedItem[index].price,
                           }])

The problem is right up here. After this check (also it’s always passing because not working) I try to remove same items from selectedItems with this code:

selectedServices.removeWhere((item) => mapEquals(
                                            item,
                                            ({
                                              'title': selectedItem[index].title,
                                              'price': selectedItem[index].price,
                                            })));

But it doesn’t work too because of the check always passing false. After these steps, my onPressed function working just on this else code bloc.

else
        {
                selectedServices.addAll([{
                selectedItem[index].title:
                selectedItem[index].price
    }
    ]);}

A little summary;
.contains method not working like this. If there is another way to set list of maps, just let me learn that. This is important for me but I can’t figure it out last few days.
And lastly, can you explain answer how it works for Dart ?

Solution

Edit: Focus of the question changed, so this answer now tries to achieve a contains like functionality for lists with complex items.

For a custom equality check a similar functionality to contains can be achieved via any:

import 'package:collection/equality.dart';

void main() {
  final map1 = {
    'title': 'item1',
    'price': 12.34,
  };

  final map2 = {
    'title': 'item2',
    'price': 3.45,
  };

  final list = [map1, map2];

  final test1 = list.any((item)=>MapEquality().equals(item, {
    'title': 'item1',
    'price': 12.34,
  }));

  print(test1);
  
  final test2 = list.any((item)=>MapEquality().equals(item, {
    'title': 'item3',
    'price': 14.34,
  }));

  print(test2);
}

print is:

true
false

you could run into precision issues with doubles, though, or if you are using complex key-value pairs in your maps.

Answered By – scrimau

Answer Checked By – Senaida (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *