restartAndRestore method

Future<void> restartAndRestore()

Simulates restoring the state of the widget tree after the application is restarted.

The method grabs the current serialized restoration data from the RestorationManager, takes down the widget tree to destroy all in-memory state, and then restores the widget tree from the serialized restoration data.

Implementation

Future<void> restartAndRestore() async {
  assert(
    binding.restorationManager.debugRootBucketAccessed,
    'The current widget tree did not inject the root bucket of the RestorationManager and '
    'therefore no restoration data has been collected to restore from. Did you forget to wrap '
    'your widget tree in a RootRestorationScope?',
  );
  return TestAsyncUtils.guard<void>(() async {
    final RootWidget widget = binding.rootElement!.widget as RootWidget;
    final TestRestorationData restorationData = binding.restorationManager.restorationData;
    runApp(Container(key: UniqueKey()));
    await pump();
    binding.restorationManager.restoreFrom(restorationData);
    binding.attachToBuildOwner(widget);
    binding.scheduleFrame();
    return binding.pump();
  });
}