ToStringOptions class
Configuration options for customizing toString
output in JetLeaf objects.
Used together with EqualsAndHashCode and the equalizer
utility to control
how objects are represented as strings. This allows fine-grained control over:
- Parameter naming (automatic, type-based, or custom)
- Multi-line vs compact formatting
- Inclusion/exclusion of class names
- Custom separators
Predefined Formats
- STANDARD →
User(id: 1, name: Alice)
- COMPACT →
User(1, Alice)
- MULTILINE →
User( id: 1, name: Alice )
- COMPACT_MULTILINE →
User( 1, Alice )
- SMART_NAMES → infers names based on values (e.g.,
"email"
,"age"
) - TYPE_BASED_NAMES → uses runtime types (e.g.,
"string"
,"int"
)
Example
class User implements EqualsAndHashCode {
final String id;
final String name;
User(this.id, this.name);
@override
List<Object?> equalizedProperties() => [id, name];
}
void main() {
final user = User('1', 'Alice');
print(equalizer.toString(user, ToStringOptions.STANDARD));
// => User(id: 1, name: Alice)
print(equalizer.toString(user, ToStringOptions.COMPACT));
// => User(1, Alice)
}
Constructors
-
ToStringOptions({bool includeParameterNames = true, bool useNewlines = false, String? customSeparator, bool includeClassName = true, List<
String> ? customParameterNames, String customParameterNameGenerator(Object? value, int index)?}) -
Configuration options for customizing
toString
output in JetLeaf objects.
Properties
- customParameterNameGenerator ↔ String Function(Object? value, int index)?
-
Custom generator for parameter names based on property values and indices.
getter/setter pair
-
customParameterNames
↔ List<
String> ? -
Explicit parameter names (overrides inference).
Must match the length of EqualsAndHashCode.equalizedProperties.getter/setter pair - customSeparator ↔ String?
-
Custom separator between parameters.
Defaults to", "
or",\n"
depending on useNewlines.getter/setter pair - hashCode → int
-
The hash code for this object.
no setterinherited
- includeClassName ↔ bool
-
Whether to include the class name in output.
getter/setter pair
- includeParameterNames ↔ bool
-
Whether to include parameter names (e.g.,
"name: Alice"
vs"Alice"
).getter/setter pair - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- useNewlines ↔ bool
-
Whether to use newlines between parameters.
getter/setter pair
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- COMPACT → ToStringOptions
-
Compact format: parameter names excluded, single-line format.
final
- COMPACT_MULTILINE → ToStringOptions
-
Multi-line format: parameter names excluded.
final
- MULTILINE → ToStringOptions
-
Multi-line format: parameter names included, each on a new line.
final
- SMART_NAMES → ToStringOptions
-
Smart name generator that infers names from common property patterns:
final
- STANDARD → ToStringOptions
-
Default options: parameter names included, single-line format.
final
- TYPE_BASED_NAMES → ToStringOptions
-
Type-based generator that uses the runtime type as parameter name.
Example:
User(string: Alice, int: 42)
final