Skip to content

Instantly share code, notes, and snippets.

@knaeckeKami
Last active February 4, 2025 15:02
Show Gist options
  • Save knaeckeKami/c3207d7526136d463760fcef954735c0 to your computer and use it in GitHub Desktop.
Save knaeckeKami/c3207d7526136d463760fcef954735c0 to your computer and use it in GitHub Desktop.
//poor man's data class
extension type const User(
({String id, String email, String firstName, String lastName}) _) {}
void main() {
final user =
User((id: "1", email: "[email protected]", firstName: "John", lastName: "Doe"));
// different instantiation, same values
final valueEqualUser =
User((id: "1", email: "[email protected]", firstName: "John", lastName: "Doe"));
// different values
final valueDifferentUser = User(
(id: "2", email: "[email protected]", firstName: "Abigail", lastName: "Moe"));
print("user == valueEqualUser ${user == valueEqualUser}");
print("user != valueDifferentUser ${user != valueDifferentUser}");
print("automatic toString: ${user.toString()}");
print("automatic hashcode: ${user.hashCode == valueEqualUser.hashCode}");
print("different hashcode: ${user.hashCode != valueDifferentUser.hashCode}");
// immutable, this is a compile time error:
// user.email = ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment