toString method
Returns a non-empty string representation of this Optional suitable for debugging.
If a value is present the result includes its string representation. Empty and present Optionals are unambiguously differentiable.
Returns the string representation of this instance.
Example
Optional<String> name = Optional.of("Olivia");
Optional<String> empty = Optional.empty();
Optional<int> number = Optional.of(42);
print(name.toString()); // "Optional[Olivia]"
print(empty.toString()); // "Optional.empty"
print(number.toString()); // "Optional[42]"
// Useful for debugging
void debugOptional(Optional<String> opt) {
print("Debug: $opt");
}
debugOptional(name); // Debug: Optional[Olivia]
debugOptional(empty); // Debug: Optional.empty
Implementation
@override
String toString() {
return _value != null ? 'Optional[$_value]' : 'Optional.empty';
}