{

Dart/Flutter:How to Compare two lists for equal Example


This tutorial shows multiple examples of how to compare two lists in Dart and Flutter programming.

Compare list numbers strings and objects for equality  in Dart or Flutter
  • Compare lists for equal
  • Check for equal comparison of the list of strings
  • how to compare a list with a string in a flutter

How to check if the given list is equal or not in Dart?

In dart, quiver package a library for collection utility reusable functions

It has a listsEqual function that compares two lists and returns true if equal else not equal.

Syntax:

bool listsEqual(List? a, List? b)

It takes two lists for comparison Return value: true or false.

Here is an example code

import 'package:quiver/collection.dart';

void main() {
  var list1 = [5, 8, 4];
  var list2 = [5, 8, 4];
  print(listsEqual(list1, list2)); // true

  var list3 = [15, 8, 4];
  var list4 = [5, 8, 4];
  print(listsEqual(list3, list4)); // false
}

Output:

true
false

How to compare two lists of numbers for equal or not in Flutter

If your application is of flutter, It has a foundation.dart library.

foundation.dart has a listEquals method, does compare each element by element in the list, and returns equal comparison result with boolean type.

bool listEquals<T>(List<T>? a, List<T>? b)
  • true: Returns true if two lists are equal with ignoring the order of elements.
  • false: Elements in the list. Here, Compare the two lists of numbers and returns true if two lists of values are equal.

Here is an example of equality for a list of numbers.

import 'package:flutter/foundation.dart';

void main() {
  var list1 = [5, 8, 4];
  var list2 = [5, 8, 4];
  print(listEquals(list1, list2)); // true
  
  var list3 = [15, 8, 4];
  var list4 = [5, 8, 4];
  print(listEquals(list3, list4)); // false
}

How to compare two lists of strings in a flutter

In the below example, list1 and list2 are equal. Hence, It returns true.

The values in each list are compared with other lists for equality.

import 'package:flutter/foundation.dart';

void main() {
  var list1 = ["one", "two", "three"];
  var list2 = ["one", "two", "three"];
  print(listEquals(list1, list2)); // true
}

In the below example, list1 and list2 are different as one of the values (“One” != “one”) is not equal. Hence, It returns false. So, It compares the exact case match for a string.

import 'package:flutter/foundation.dart';

void main() {
  var list1 = ["One", "two", "three"];
  var list2 = ["one", "two", "three"];
  print(listEquals(list1, list2)); // false
}

How to compare two lists of Objects for equal or in Flutter

this shows you compare two lists of objects, returns if two lists are equal, else returns false.

Following are the steps required.

  • Created Employee with id, name, salary, and joinDate
  • Created four lists in the below example
  • Compared two lists using
import 'package:flutter/foundation.dart';
import 'dart:convert';

class Employee {
  final int id;
  final String name;
  final int salary;
  final DateTime joinDate;

  Employee(this.id, this.name, this.salary, this.joinDate);

  Map<String, dynamic> toJson() =>
      {"id": id, "name": name, "salary": salary, "doj": joinDate.toString()};
}

void main() {
  final e1 =
      Employee(11, 'Erwin', 9000, DateTime.parse("2020-06-21 00:00:00.000"));
  final e2 =
      Employee(21, 'Andrew', 70000, DateTime.parse("2021-07-23 00:00:00.000"));
  final e3 =
      Employee(4, 'Mark', 8000, DateTime.parse("2017-08-24 00:00:00.000"));
  final e4 =
      Employee(12, 'Otroc', 5000, DateTime.parse("2022-12-05 00:00:00.000"));

  final List<Employee> employees = [e1, e2, e3, e4];
  final List<Employee> employees1 = [e1, e2, e3, e4];
  print(listEquals(employees, employees1)); // true

  final List<Employee> employees2 = [e1, e3, e4];
  final List<Employee> employees3 = [e1, e2, e4];
  print(listEquals(employees2, employees3)); // false
}

Conclusion

In this tutorial, Learned how to compare two lists of numbers, strings, and objects equality in Dart and flutter programming

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example