Home > Blockchain >  Flutter How to add map to list?
Flutter How to add map to list?

Time:03-22

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 ?

CodePudding user response:

This code works as expected imo:

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];
  
  list.removeWhere((item)=>MapEquality().equals(item, {
    'title': 'item1',
    'price': 12.34,
  }));
  
  print(list);
}

print is:

[{title: item2, price: 3.45}]

which i would expect is what you want to do, but i could be mistaken...

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

CodePudding user response:

Don't you try to check if array contains other array in this code:

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

?

Try to check with one item in parameter-list:

if (selectedServices.contains(
          {'title': selectedItem[index].title}
    ))
  • Related