skip to Main Content

I get a LateInitializationError on items_head after resizing my widget.

This is code:

class Head_View extends StatefulWidget {
  Head_View({Key? key}) : super(key: key);
  late List<AppointmentItemWrapper> items_head;
  //late MultiChildRenderObjectWidget DTIT_head; --> **EDIT ON 20230605**
  @override
  State<Head_View> createState() => _Head_ViewState();
}

class _Head_ViewState extends State<Head_View> {
  @override
  void initState() {

    widget.items_head = [
      AppointmentItemWrapper(
        ID: 111,
        key: UniqueKey(),
        startDateTime: DateTime(2022, 1, 1, 8, 00, 0),
        endDateTime: DateTime(2022, 1, 1, 9, 30,0),
        position: 0,
        text: "Event N1",
        positiondragging: (double valxx) { print("HAHAH"); },
      ),
    ];

    super.initState();
  }

  @override
  Widget build(BuildContext context) {

    return DynamicTimeline(
      key: _myWidgetKey_DTIT_head,
      firstDateTime: DateTime(2022, 1, 1,7,0,0),
      lastDateTime: DateTime(2022, 1, 1,18,0,0),
      labelBuilder: DateFormat('hh:mm').format,
      items: widget.items_head, );
  }
}

As I thought:

initState() should be normally only called before first build (first build would happen in super.init()? )and when resizing the widget, build should be called again to rebuild the widget, but the items_head should be already in scope or am I wrong?

2

Answers


  1. You should initialize the variables in the state widget when using the stateful widget type.

    Move the items_head below to the state widget.

    class Head_View extends StatefulWidget {
      Head_View({Key? key}) : super(key: key);
      @override
      State<Head_View> createState() => _Head_ViewState();
    }
    
    class _Head_ViewState extends State<Head_View> {
      
      late List<AppointmentItemWrapper> items_head;
    
    @override
      void initState() {
    
        items_head = [
          AppointmentItemWrapper(
            ID: 111,
            key: UniqueKey(),
            startDateTime: DateTime(2022, 1, 1, 8, 00, 0),
            endDateTime: DateTime(2022, 1, 1, 9, 30,0),
            position: 0,
            text: "Event N1",
            positiondragging: (double valxx) { print("HAHAH"); },
          ),
        ];
    
        super.initState();
      }
    
    ....
    
    Login or Signup to reply.
  2. if you declare items_head in class Head_View extends StatefulWidget {

    it will empty the List every time it rebuild.

    try to declare here.

    class _Head_ViewState extends State<Head_View> {
    
      late List<AppointmentItemWrapper> items_head;// **in here**
    
      @override
      void initState() {
    
        widget.items_head = [
          AppointmentItemWrapper(
            ID: 111,
            key: UniqueKey(),
            startDateTime: DateTime(2022, 1, 1, 8, 00, 0),
            endDateTime: DateTime(2022, 1, 1, 9, 30,0),
            position: 0,
            text: "Event N1",
            positiondragging: (double valxx) { print("HAHAH"); },
          ),
        ];
    
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
    
        return DynamicTimeline(
          key: _myWidgetKey_DTIT_head,
          firstDateTime: DateTime(2022, 1, 1,7,0,0),
          lastDateTime: DateTime(2022, 1, 1,18,0,0),
          labelBuilder: DateFormat('hh:mm').format,
          items: widget.items_head, );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search