toString method

  1. @override
String toString()
override

Never throws: a key whose toString throws, or a describe that does, is printed as a mark, Job(key: <describe threw StateError>).

See A job's name on the observing page.

Implementation

@override
String toString() {
  String part(String what, String Function() print) {
    try {
      return print();
    } on Object catch (error, stackTrace) {
      _marked?.add((error, stackTrace));
      return '<$what threw ${_typeOf(error)}>';
    }
  }

  final key = _key;
  if (key == null) {
    // Not `Job(null)`: a job without a key has no name to print, and a
    // `null` in every error message reads as a defect of its own.
    return 'Job(${part('describe', describe)})';
  }
  // The key first, the way it is printed, and so its error is reported
  // first too.
  final name = part('key', () => '$key');
  final description = part('describe', describe);
  return description.isEmpty ? 'Job($name)' : 'Job($name: $description)';
}