StatefulElement constructor Null safety

StatefulElement(
  1. StatefulWidget widget
)

Creates an element that uses the given widget as its configuration.

Implementation

StatefulElement(StatefulWidget widget)
    : _state = widget.createState(),
      super(widget) {
  assert(() {
    if (!state._debugTypesAreRight(widget)) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary('StatefulWidget.createState must return a subtype of State<${widget.runtimeType}>'),
        ErrorDescription(
          'The createState function for ${widget.runtimeType} returned a state '
          'of type ${state.runtimeType}, which is not a subtype of '
          'State<${widget.runtimeType}>, violating the contract for createState.',
        ),
      ]);
    }
    return true;
  }());
  assert(state._element == null);
  state._element = this;
  assert(
    state._widget == null,
    'The createState function for $widget returned an old or invalid state '
    'instance: ${state._widget}, which is not null, violating the contract '
    'for createState.',
  );
  state._widget = widget;
  assert(state._debugLifecycleState == _StateLifecycle.created);
}