7.4 C
New York
Friday, December 1, 2023

Flutter: 2 Ways to Compare 2 Deep Nested Maps

2 maps are considered equal if they have the same length, and contain the same keys associated with the same values. This article shows you 2 ways to correctly compare 2 deep nested maps in Flutter. Without any further ado, letÂ’s examine the code.

Using equals() method (DeepCollectionEquality class)

The equals method of the DeepCollectionEquality class can help us compare 2 nested maps with a few lines of code. To use it, you need to import the collection library:Advertisements

import 'package:collection/collection.dart';

Example

LetÂ’s say we have 3 maps: item 1, item 2, and item 3. Our job is to compare the first item with the second and the third one.

// main.dart
import 'package:collection/collection.dart';

Map<String, dynamic> item1 = {
  'name': 'T-shirt',
  'color': 'blue',
  'price': 10.99,
  'details': {
    'department': 'men',
    'manufactuer': 'KindaCode.com',
    'style': 'classic',
    'size': ['S', 'M', 'L', 'XL', '2XL', '3XL'],
    'material': 'cotton',
    'ship': ['Standart', 'FedEx Air']
  }
};

Map<String, dynamic> item2 = {
  'name': 'T-shirt',
  'color': 'blue',
  'price': 10.99,
  'details': {
    'department': 'men',
    'manufactuer': 'KindaCode.com',
    'style': 'classic',
    'size': ['S', 'M', 'L', 'XL', '2XL', '3XL'],
    'material': 'cotton',
    'ship': ['Standart', 'FedEx Air']
  }
};

Map<String, dynamic> item3 = {
  'name': 'T-shirt',
  'color': 'blue',
  'price': 10.99,
  'details': {
    'department': 'mens',
    'manufactuer': 'KindaCode.com',
    'style': 'classic',
    'size': ['S', 'M', 'L', 'XL'],
    'material': 'cotton',
    'ship': ['Standart', 'FedEx Air']
  }
};

void main() {
  // Comparing item #1 vs item #2
  if (DeepCollectionEquality().equals(item1, item2)) {
    print('Item #1 and item #2 are equal');
  } else {
    print('Item #1 and item #2 are not equal');
  }

  // Comparing item #1 vs item #3
  if (DeepCollectionEquality().equals(item1, item3)) {
    print('Item #1 and item #3 are equal');
  } else {
    print('Item #1 and item #3 are not equal');
  }
}

Output:

Item #1 and item #2 are equal
Item #1 and item #3 are not equal

If you look closer, item 3 doesnÂ’t go with 2XL size. ThatÂ’s why it is different from the others.

Converting Maps to JSON

AdvertisementsThe strategy here is to convert each map to a JSON string then compare the returned strings.

Example

WeÂ’ll use the same maps as the first example.

// main.dart
import 'dart:convert';

Map<String, dynamic> item1 = {
  'name': 'T-shirt',
  'color': 'blue',
  'price': 10.99,
  'details': {
    'department': 'men',
    'manufactuer': 'KindaCode.com',
    'style': 'classic',
    'size': ['S', 'M', 'L', 'XL', '2XL', '3XL'],
    'material': 'cotton',
    'ship': ['Standart', 'FedEx Air']
  }
};

Map<String, dynamic> item2 = {
  'name': 'T-shirt',
  'color': 'blue',
  'price': 10.99,
  'details': {
    'department': 'men',
    'manufactuer': 'KindaCode.com',
    'style': 'classic',
    'size': ['S', 'M', 'L', 'XL', '2XL', '3XL'],
    'material': 'cotton',
    'ship': ['Standart', 'FedEx Air']
  }
};

Map<String, dynamic> item3 = {
  'name': 'T-shirt',
  'color': 'blue',
  'price': 10.99,
  'details': {
    'department': 'mens',
    'manufactuer': 'KindaCode.com',
    'style': 'classic',
    'size': ['S', 'M', 'L', 'XL'],
    'material': 'cotton',
    'ship': ['Standart', 'FedEx Air']
  }
};

void main() {
  // Converting maps to strings
  String s1 = json.encode(item1);
  String s2 = json.encode(item2);
  String s3 = json.encode(item3);

  // Comparing item #1 vs item #2
  if (s1 == s2) {
    print('Item #1 and item #2 are equal');
  } else {
    print('Item #1 and item #2 are not equal');
  }

  // Comparing item #1 vs item #3
  if (s1 == s3) {
    print('Item #1 and item #3 are equal');
  } else {
    print('Item #1 and item #3 are not equal');
  }
}

Output:

Item #1 and item #2 are equal
Item #1 and item #3 are not equal

WhatÂ’s Next?

Advertisements

WeÂ’ve gone through a few techniques to deep compare two different maps in Flutter. These things may be very useful for you in certain situations in the future when you work with massive data apps.

Flutter is awesome and there are so many great features to explore. Keep yourself moving and continue learning by taking a look at the following articles:

You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.

Advertisements

Related Articles

Latest Articles