toString method

  1. @override
String toString()
override

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Implementation

@override
String toString() {
  String builder = clean == Rational.zero ? '' : clean.toString();
  for (final dirtyPart in dirtyParts.entries) {
    builder += (dirtyPart.value.sign == 1 ? '+' : '-');
    if (dirtyPart.value.abs() != 1) {
      final quantity = dirtyPart.value.abs();
      if (quantity.round() == quantity) {
        builder += quantity.toInt().toString();
      } else {
        builder += quantity.toString();
      }
    }
    builder += dirtyPart.key.representation;
  }
  if (clean == Rational.zero) {
    if (dirtyParts.isEmpty) {
      builder = '0';
    } else if (builder.startsWith('+')) {
      builder = builder.substring(1);
    }
  }
  return builder;
}