void finalizeTree()

Complete the element build pass by unmounting any elements that are no longer active.

This is called by WidgetsBinding.beginFrame.

In checked mode, this also verifies that each global key is used at most once.

After the current call stack unwinds, a microtask that notifies listeners about changes to global keys will run.

Source

void finalizeTree() {
  Timeline.startSync('Finalize tree');
  try {
    lockState(() {
      _inactiveElements._unmountAll();
    });
    assert(GlobalKey._debugCheckForDuplicates);
    scheduleMicrotask(GlobalKey._notifyListeners);
  } catch (e, stack) {
    _debugReportException('while finalizing the widget tree', e, stack);
  } finally {
    Timeline.finishSync();
  }
}