@@ -25,3 +25,11 @@ class CreatePlayerComplete extends CreatePlayerState {
...
@@ -25,3 +25,11 @@ class CreatePlayerComplete extends CreatePlayerState {
In the code snippet above, we have an abstract class called CreatePlayerState and three concrete states: CreatePlayerInitial, CreatePlayerLoading, and CreatePlayerComplete. The Equatable package provides an easy way to define equality comparison methods in Dart. In the CreatePlayerState abstract class, the props method is used to check for instance equality. Instances are considered equal if they are instances of the CreatePlayerState class, and no instance variables are needed to evaluate for equality.
In the code snippet above, we have an abstract class called CreatePlayerState and three concrete states: CreatePlayerInitial, CreatePlayerLoading, and CreatePlayerComplete. The Equatable package provides an easy way to define equality comparison methods in Dart. In the CreatePlayerState abstract class, the props method is used to check for instance equality. Instances are considered equal if they are instances of the CreatePlayerState class, and no instance variables are needed to evaluate for equality.
In the CreatePlayerComplete class, there is a single instance variable called response, which is initialized by the constructor. The props list at the end of the class definition is used to determine whether two instances of the CreatePlayerComplete class are equal. In this case, the instances are considered equal if they are instances of the CreatePlayerComplete class and the value of their response instance variable is the same.
In the CreatePlayerComplete class, there is a single instance variable called response, which is initialized by the constructor. The props list at the end of the class definition is used to determine whether two instances of the CreatePlayerComplete class are equal. In this case, the instances are considered equal if they are instances of the CreatePlayerComplete class and the value of their response instance variable is the same.
### Quick Note on @immutable
By adding the @immutable annotation to the abstract class, this indicates that any class that implements the immutable abstract class will remain the same throughout its lifetime.
In Flutter, @immutable is often used to make UI elements more efficient. Since UI elements are constantly being rebuilt, it is important that their state can be compared quickly and efficiently. By using @immutable, Dart can optimize the comparison process by only comparing the object's reference rather than its properties.
In the particular example previously method on this page, by marking the CreatePlayerState abstract class as immutable, any states that are instances of CreatePlayerState cannot be changed once they are created. This is important because the state of a widget in Flutter should only be changed by creating a new instance of a state object, rather than modifying the existing one.