3

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!

2
  • 1
    Is this your exact code? I can't reproduce your problem with the code you've shown. expect implicitly uses the equals matcher, and the equals matcher checks for Lists and performs an element-wise comparison for equality.
    – jamesdlin
    Commented Mar 22, 2022 at 20:02
  • I see that you've already accepted an answer, but could you please provide a reproducible example? Even if the accepted answer seems to fix your problem, there probably is something else that's wrong since the code you've shown should already be working.
    – jamesdlin
    Commented Mar 22, 2022 at 20:53

2 Answers 2

3

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);
1
  • 6
    While true, this shouldn't matter when using expect and its implict equals matcher.
    – jamesdlin
    Commented Mar 22, 2022 at 20:04
0

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));
}

1
  • 3
    While true, this shouldn't matter when using expect and its implict equals matcher.
    – jamesdlin
    Commented Mar 22, 2022 at 20:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.