createState method Null safety

  1. @override
_State<T, R> createState()
override

Creates the mutable state for this widget at a given location in the tree.

Subclasses should override this method to return a newly created instance of their associated State subclass:

@override
State<MyWidget> createState() => _MyWidgetState();

The framework can call this method multiple times over the lifetime of a StatefulWidget. For example, if the widget is inserted into the tree in multiple locations, the framework will create a separate State object for each location. Similarly, if the widget is removed from the tree and later inserted into the tree again, the framework will call createState again to create a fresh State object, simplifying the lifecycle of State objects.

Implementation

@override
// ignore: no_logic_in_create_state
_State<T, R> createState() {
  switch (mixinWith) {
    case MixinWith.singleTickerProviderStateMixin:
      assert(
          (initState != null || afterInitialBuild != null) && dispose != null,
          '''
initState` `dispose` must not be null because you are using SingleTickerProviderStateMixin
and you are supposed to to instantiate your controllers in the initState() and dispose them
 in the dispose() method'
      ''');
      return _StateWithSingleTickerProvider<T, R>();
    case MixinWith.tickerProviderStateMixin:
      assert(
          (initState != null || afterInitialBuild != null) && dispose != null,
          '''
initState` `dispose` must not be null because you are using TickerProviderStateMixin
and you are supposed to to instantiate your controllers in the initState() and dispose them
 in the dispose() method'
      ''');
      return _StateWithTickerProvider<T, R>();
    case MixinWith.automaticKeepAliveClientMixin:
      return _StateWithKeepAliveClient<T, R>();
    case MixinWith.widgetsBindingObserver:
      return _StateWithWidgetsBindingObserver<T, R>();
  }
}