diff --git a/lib/src/equatable_utils.dart b/lib/src/equatable_utils.dart index 615b0020..575ab489 100644 --- a/lib/src/equatable_utils.dart +++ b/lib/src/equatable_utils.dart @@ -21,6 +21,10 @@ bool iterableEquals(Iterable a, Iterable b) { return true; } +/// Determines whether two numbers are equal. +@pragma('vm:prefer-inline') +bool numEquals(num a, num b) => a == b; + /// Determines whether two sets are equal. bool setEquals(Set a, Set b) { if (identical(a, b)) return true; @@ -45,7 +49,9 @@ bool mapEquals(Map a, Map b) { @pragma('vm:prefer-inline') bool objectsEquals(Object? a, Object? b) { if (identical(a, b)) return true; - if (_isEquatable(a) && _isEquatable(b)) { + if (a is num && b is num) { + return numEquals(a, b); + } else if (_isEquatable(a) && _isEquatable(b)) { return a == b; } else if (a is Set && b is Set) { return setEquals(a, b); diff --git a/test/equatable_utils_test.dart b/test/equatable_utils_test.dart index bea9f886..f842b335 100644 --- a/test/equatable_utils_test.dart +++ b/test/equatable_utils_test.dart @@ -262,5 +262,22 @@ void main() { test('returns false for different types', () { expect(objectsEquals(1, '1'), isFalse); }); + + test('returns true when two nums have the same value (int and double)', () { + const num a = 0; + const num b = 0.0; + expect(objectsEquals(a, b), isTrue); + }); + + test('returns true when two nums are identical', () { + const num a = 0; + expect(objectsEquals(a, a), isTrue); + }); + + test('returns false when two nums have different values', () { + const num a = 0; + const num b = 1; + expect(objectsEquals(a, b), isFalse); + }); }); }