waitForVeloState<T extends Velo, S> method

Future<void> waitForVeloState<T extends Velo, S>(
  1. S expectedState, {
  2. Duration timeout = const Duration(seconds: 5),
})

Waits for a specific state to be present in a VeloBuilder.

Implementation

Future<void> waitForVeloState<T extends Velo<dynamic>, S>(
  S expectedState, {
  Duration timeout = const Duration(seconds: 5),
}) async {
  ArgumentError.checkNotNull(expectedState, 'expectedState');
  ArgumentError.checkNotNull(timeout, 'timeout');

  if (timeout.isNegative) {
    throw ArgumentError.value(timeout, 'timeout', 'Cannot be negative');
  }

  try {
    await waitFor(() {
      try {
        final context = element(find.byType(MaterialApp));
        final velo = Provider.of<T>(context, listen: false);
        return velo.state == expectedState;
      } on ProviderNotFoundException catch (e) {
        throw StateError(
          'Velo of type $T not found in widget tree. '
          'Make sure to provide it using pumpVeloWidget or similar. '
          'Original error: $e',
        );
      }
    }, timeout: timeout);
  } catch (e) {
    throw StateError('Failed to wait for Velo state $expectedState: $e');
  }
}