inflateWidget method Null safety

  1. @protected
Element inflateWidget(
  1. Widget newWidget,
  2. Object? newSlot
)
protected">@protected

Create an element for the given widget and add it as a child of this element in the given slot.

This method is typically called by updateChild but can be called directly by subclasses that need finer-grained control over creating elements.

If the given widget has a global key and an element already exists that has a widget with that global key, this function will reuse that element (potentially grafting it from another location in the tree or reactivating it from the list of inactive elements) rather than creating a new element.

The newSlot argument specifies the new value for this element's slot.

The element returned by this function will already have been mounted and will be in the "active" lifecycle state.

Implementation

@protected
@pragma('vm:prefer-inline')
Element inflateWidget(Widget newWidget, Object? newSlot) {
  assert(newWidget != null);

  final bool isTimelineTracked = !kReleaseMode && _isProfileBuildsEnabledFor(newWidget);
  if (isTimelineTracked) {
    Map<String, String>? debugTimelineArguments;
    assert(() {
      if (kDebugMode && debugEnhanceBuildTimelineArguments) {
        debugTimelineArguments = newWidget.toDiagnosticsNode().toTimelineArguments();
      }
      return true;
    }());
    Timeline.startSync(
      '${newWidget.runtimeType}',
      arguments: debugTimelineArguments,
    );
  }

  try {
    final Key? key = newWidget.key;
    if (key is GlobalKey) {
      final Element? newChild = _retakeInactiveElement(key, newWidget);
      if (newChild != null) {
        assert(newChild._parent == null);
        assert(() {
          _debugCheckForCycles(newChild);
          return true;
        }());
        newChild._activateWithParent(this, newSlot);
        final Element? updatedChild = updateChild(newChild, newWidget, newSlot);
        assert(newChild == updatedChild);
        return updatedChild!;
      }
    }
    final Element newChild = newWidget.createElement();
    assert(() {
      _debugCheckForCycles(newChild);
      return true;
    }());
    newChild.mount(this, newSlot);
    assert(newChild._lifecycleState == _ElementLifecycle.active);

    return newChild;
  } finally {
    if (isTimelineTracked) {
      Timeline.finishSync();
    }
  }
}