matchAsync method

  1. @override
dynamic matchAsync(
  1. dynamic item

Returns null if this matches item, or a String description of the failure if it doesn't match.

This can return a Future or a synchronous value. If it returns a Future, neither expect nor the test will complete until that Future completes.

If this returns a String synchronously, expect will synchronously throw a TestFailure and matches will synchronusly return false.


dynamic /*FutureOr<String>*/ matchAsync(item) {
  if (item is! Function && item is! Future) {
    return 'was not a Function or Future';

  if (item is Future) {
    return item.then((value) => indent(prettyPrint(value), first: 'emitted '),
        onError: _check);

  try {
    var value = item();
    if (value is Future) {
      return value.then(
          (value) => indent(prettyPrint(value),
              first: 'returned a Future that emitted '),
          onError: _check);

    return indent(prettyPrint(value), first: 'returned ');
  } catch (error, trace) {
    return _check(error, trace);