getControllerSync method
Synchronously retrieves a controller by name without recreation.
Unlike getController, this method only returns existing, non-disposed controllers. It will not attempt to recreate controllers that were disposed, making it suitable for cases where you need immediate access without the asynchronous delay of potential recreation.
This method updates the usage order of returned controllers, keeping them from being disposed due to inactivity.
name The unique name of the controller to retrieve.
Returns the controller if found and not disposed, null otherwise.
Implementation
WebFController? getControllerSync(String name) {
if (_controllersByName.containsKey(name)) {
final instance = _controllersByName[name]!;
// If controller is disposed, remove it and return null
if (instance.state == ControllerState.disposed) {
// Remove from tracking collections to prevent memory leaks
_controllersByName.remove(name);
_recentlyUsedControllers.removeWhere((element) => element == name);
_attachedControllers.removeWhere((element) => element == name);
return null;
}
// Update usage order for non-disposed controllers
_updateUsageOrder(name);
return instance.controller;
}
return null;
}