Gavin Belson
Gavin Belson

Reputation: 91

How do I compare two list in expect function of flutter unit testing?

List<int> l1=[1,2,3]; List<int> l2=[1,2,3]; expect (l1,l2);

This is the code I'm using in Flutter unit testing.

Eventhough both of the list has the same content I'm not able to pass the test. I'm not able to find the usecase of comparing lists using Equatable in Flutter unit testing. Can someone please help? Thanks!

Upvotes: 3

Views: 3860

Answers (2)

quoci
quoci

Reputation: 3557

You can use the method ListEquality().equals() to check if two List are equal.

import 'package:collection/collection.dart';

List<int> l1 = [1,2,3];  
List<int> l2 = [1,2,3];
final bool equal = ListEquality().equals(l1, l2);
expect(equal, true);

Upvotes: 3

salihgueler
salihgueler

Reputation: 3622

List equality works differently in Dart. Since everything is an object you need a mechanism to check each element.

You can use either the ListEquality class to compare two lists if they do not have nested objects/informations or, if you want to compare nested objects/informations you can use DeepCollectionEquality. Both come from collections library which comes out of the box with Dart.

You can check the following examples of the usages:

import 'package:collection/collection.dart';

void main() {
  const numberListOne = [1,2,3];
  const numberListTwo = [1,2,3];
  
  final _listEquality = ListEquality();
  print(_listEquality.equals(numberListOne, numberListTwo));
}

Upvotes: 0

Related Questions