ToString mixin

A mixin that provides customizable toString implementation using the same properties defined in EqualsAndHashCode.equalizedProperties.

This mixin automatically generates toString output based on the properties used for equality comparison, with various formatting options.

Example

class Person with EqualsAndHashCode, ToString {
  final String name;
  final int age;
  final String email;

  Person(this.name, this.age, this.email);

  @override
  List<Object?> equalizedProperties() => [name, age, email];
  
  @override
  List<String> get propertyNames => ['name', 'age', 'email'];
}

void main() {
  final person = Person('Alice', 25, 'alice@example.com');
  
  // Default: Person(name: Alice, age: 25, email: alice@example.com)
  print(person.toString());
  
  // Compact: Person(Alice, 25, alice@example.com)
  print(person.toStringWith(ToStringOptions.compact));
  
  // Multi-line:
  // Person(
  //   name: Alice,
  //   age: 25,
  //   email: alice@example.com
  // )
  print(person.toStringWith(ToStringOptions.multiline));
}
Superclass constraints

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

equalizedProperties() List<Object?>
Mixin-style contract for value-based equality, hashCode, and toString.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override
toStringOptions() ToStringOptions

Operators

operator ==(Object other) bool
The equality operator.
inherited