waitForWidget function

Future<WaitResult> waitForWidget(
  1. WaitInput input
)

Waits until a widget or route is present or absent in the running Flutter app.

Never throws; all error conditions are represented as sealed result cases.

Implementation

Future<WaitResult> waitForWidget(WaitInput input) async {
  try {
    final isolateId = await checkFdbHelper();
    if (isolateId == null) return const WaitNoFdbHelper();

    final params = <String, String>{
      'isolateId': isolateId,
      'condition': input.condition.name,
      'timeout': input.timeoutMs.toString(),
    };
    if (input.text != null) params['text'] = input.text!;
    if (input.key != null) params['key'] = input.key!;
    if (input.type != null) params['type'] = input.type!;
    if (input.route != null) params['route'] = input.route!;

    final result = await fdbWaitFor(
      params,
      timeout: Duration(milliseconds: input.timeoutMs),
    );

    if (result.isSuccess) {
      final token = _selectorToken(input.key, input.text, input.type, input.route);
      return WaitConditionMet(condition: input.condition, selectorToken: token);
    }

    if (result.error != null) return WaitRelayedError(result.error!);

    return WaitUnexpectedResponse(result.unexpected);
  } on AppDiedException catch (e) {
    return WaitAppDied(logLines: e.logLines, reason: e.reason);
  } catch (e) {
    return WaitError(e.toString());
  }
}