Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Value Equality and Comparison in Dart | Writing Clean Dart for Scalable Flutter Apps
Dart for Flutter Developers

bookValue Equality and Comparison in Dart

Understanding how Dart compares objects is crucial for writing reliable and maintainable Flutter apps. Dart distinguishes between reference equality and value equality. Reference equality checks whether two variables point to the exact same object in memory, using the == operator by default if not overridden. Value equality, on the other hand, checks whether two objects contain the same data or properties, regardless of whether they are the same instance. Without explicitly overriding equality in your classes, Dart will only compare object references, not their contents.

value_equality_example.dart

value_equality_example.dart

copy
1234567891011121314151617181920212223
class User { final String name; final int age; User(this.name, this.age); @override bool operator ==(Object other) => identical(this, other) || other is User && runtimeType == other.runtimeType && name == other.name && age == other.age; @override int get hashCode => name.hashCode ^ age.hashCode; } void main() { var user1 = User('Alice', 30); var user2 = User('Alice', 30); print(user1 == user2); // true }

In Flutter, value equality is especially important for state management and widget rebuilding. Flutter relies on object comparisons to determine if it needs to update the UI. If your data classes only use reference equality, Flutter might not recognize changes in state, leading to stale or incorrect UI. By implementing value equality, you ensure that Flutter can accurately detect changes and update widgets as needed.

flutter_list_comparison.dart

flutter_list_comparison.dart

copy
12345678910111213141516171819202122232425262728
// flutter_list_comparison.dart class TodoItem { final String task; final bool completed; TodoItem(this.task, this.completed); @override bool operator ==(Object other) => identical(this, other) || other is TodoItem && runtimeType == other.runtimeType && task == other.task && completed == other.completed; @override int get hashCode => task.hashCode ^ completed.hashCode; } void main() { var list = [ TodoItem('Buy milk', false), TodoItem('Walk dog', true), ]; var newItem = TodoItem('Buy milk', false); print(list.contains(newItem)); // true, thanks to value equality }
Note
Note

By always implementing value equality (== and hashCode) in your custom classes, you avoid subtle bugs where collections like List, Set, or Map fail to recognize logically identical objects. This is essential for predictable behavior in Flutter apps, especially when working with state or caching.

question mark

Which statement about value equality in Dart is correct?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain how to implement value equality in Dart classes?

Why is overriding the equality operator important in Flutter apps?

What are some common mistakes when comparing objects in Dart?

bookValue Equality and Comparison in Dart

Stryg for at vise menuen

Understanding how Dart compares objects is crucial for writing reliable and maintainable Flutter apps. Dart distinguishes between reference equality and value equality. Reference equality checks whether two variables point to the exact same object in memory, using the == operator by default if not overridden. Value equality, on the other hand, checks whether two objects contain the same data or properties, regardless of whether they are the same instance. Without explicitly overriding equality in your classes, Dart will only compare object references, not their contents.

value_equality_example.dart

value_equality_example.dart

copy
1234567891011121314151617181920212223
class User { final String name; final int age; User(this.name, this.age); @override bool operator ==(Object other) => identical(this, other) || other is User && runtimeType == other.runtimeType && name == other.name && age == other.age; @override int get hashCode => name.hashCode ^ age.hashCode; } void main() { var user1 = User('Alice', 30); var user2 = User('Alice', 30); print(user1 == user2); // true }

In Flutter, value equality is especially important for state management and widget rebuilding. Flutter relies on object comparisons to determine if it needs to update the UI. If your data classes only use reference equality, Flutter might not recognize changes in state, leading to stale or incorrect UI. By implementing value equality, you ensure that Flutter can accurately detect changes and update widgets as needed.

flutter_list_comparison.dart

flutter_list_comparison.dart

copy
12345678910111213141516171819202122232425262728
// flutter_list_comparison.dart class TodoItem { final String task; final bool completed; TodoItem(this.task, this.completed); @override bool operator ==(Object other) => identical(this, other) || other is TodoItem && runtimeType == other.runtimeType && task == other.task && completed == other.completed; @override int get hashCode => task.hashCode ^ completed.hashCode; } void main() { var list = [ TodoItem('Buy milk', false), TodoItem('Walk dog', true), ]; var newItem = TodoItem('Buy milk', false); print(list.contains(newItem)); // true, thanks to value equality }
Note
Note

By always implementing value equality (== and hashCode) in your custom classes, you avoid subtle bugs where collections like List, Set, or Map fail to recognize logically identical objects. This is essential for predictable behavior in Flutter apps, especially when working with state or caching.

question mark

Which statement about value equality in Dart is correct?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3
some-alt