restart method

Future<Duration> restart({
  1. Duration timeout = const Duration(seconds: 60),
})

Hot restarts the app through the Flutter tool that launched it, then finds the new Nylo isolate.

Implementation

Future<Duration> restart({
  Duration timeout = const Duration(seconds: 60),
}) async {
  final String? method = await toolServiceMethod('hotRestart');
  if (method == null) {
    throw LiveCommandException(
      'Hot restart isn\'t available. The app needs to still be attached to '
      '`flutter run` or your IDE.',
    );
  }
  final String previous = isolateId;
  final Stopwatch stopwatch = Stopwatch()..start();
  await client.call(method, const {}, timeout);

  final DateTime deadline = DateTime.now().add(timeout);
  while (DateTime.now().isBefore(deadline)) {
    final String? fresh = await findNyloIsolate(
      client,
      timeout: client.timeout,
    ).catchError((Object _) => null);
    if (fresh != null && fresh != previous) {
      isolateId = fresh;
      break;
    }
    await Future<void>.delayed(const Duration(milliseconds: 150));
  }
  if (isolateId == previous) {
    throw LiveCommandException(
      'The app restarted but Nylo Live didn\'t come back within '
      '${timeout.inSeconds}s.',
    );
  }

  final Object? fresh = await call('status');
  if (fresh is Map) status = Map<String, Object?>.from(fresh);
  return stopwatch.elapsed;
}