-1

I have problems removing repeated data from a nested list, I need to remove the records that are not repeated in the index 0 of the list, but the Set method is not doing anything when the list is nested.

for (int i = 0; i < items.length; i++) {
      sections.add([items[i].sectionG, items[i].logo]);
    }

    sections = sections.toSet().toList();

Imagen de la estructura de la lista

When I go through Set I get back the same structure as before.

Richard Wilson
  • 297
  • 4
  • 17

1 Answers1

0

operator == for most Dart objects, including Lists, Sets, and Maps, checks only for object identity and does not perform deep equality checks. That is, two separate List objects do not compare equal even if they contain the same elements.

You will need to create a Set that performs deep equality checks. package:collection provides an EqualitySet class that can use a ListEquality object to compare List elements:

import 'package:collection/collection.dart';

void main() {
  var listOfLists = [
    [1, 2, 3],
    [1, 2],
    [1, 2, 3, 4],
    [1, 2, 3],
  ];
  var set = EqualitySet.from(ListEquality<int>(), listOfLists);
  print(set); // Prints: {[1, 2, 3], [1, 2], [1, 2, 3, 4]}
}
jamesdlin
  • 81,374
  • 13
  • 159
  • 204