loaded method

Future<void> loaded([
  1. GameObject? object,
  2. Duration timeout = const Duration(seconds: 30)
])

Waits until object (the game on screen unless given) has loaded what its onLoad loads, and everything added then, and throws its load's error if it failed: await tester.loaded(). The files are read in real time, which a widget test's frames don't move, so it waits a little in real time and pumps a frame, over and over: the game runs meanwhile, as it would. Throws a TimeoutException after timeout of real time.

await tester.runAsync(() => game.loaded) would wait for ever: the game's onLoad goes on after each await only when a frame is pumped.

Implementation

Future<void> loaded([GameObject? object, Duration timeout = const Duration(seconds: 30)]) async {
  final target = object ?? game<GameObject>();
  (Object, StackTrace)? failed;
  var done = false;
  unawaited(
    target.loaded.then(
      (_) => done = true,
      onError: (Object error, StackTrace stack) {
        failed = (error, stack);
        done = true;
      },
    ),
  );
  await _until(() => done, timeout, () => '$target had not loaded after ${timeout.inSeconds} s');
  if (failed case (final error, final stack)) Error.throwWithStackTrace(error, stack);
}