context property Null safety

BuildContext context

The location in the tree where this widget builds.

The framework associates State objects with a BuildContext after creating them with StatefulWidget.createState and before calling initState. The association is permanent: the State object will never change its BuildContext. However, the BuildContext itself can be moved around the tree.

After calling dispose, the framework severs the State object's connection with the BuildContext.

Implementation

BuildContext get context {
  assert(() {
    if (_element == null) {
      throw FlutterError(
        'This widget has been unmounted, so the State no longer has a context (and should be considered defunct). \n'
        'Consider canceling any active work during "dispose" or using the "mounted" getter to determine if the State is still active.',
      );
    }
    return true;
  }());
  return _element!;
}