skip to Main Content

I am trying to transfer data from the first page to the second page like this.

Navigator.pushReplacement(context, MaterialPageRoute(builder: (contex) => SegmentationSchedule(idBatch: batchTransfer.id!,)));

but when I try to retrieve the data a message like this appears

The instance member 'widget' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression

this is how I display the data

class SegmentationSchedule extends StatefulWidget {
  int idBatch;
  SegmentationSchedule({super.key, required this.idBatch});
  @override
  State<SegmentationSchedule> createState() => _SegmentationScheduleState();
}

class _SegmentationScheduleState extends State<SegmentationSchedule> {
  int groupValue = 0;

  List<Widget> bodies = [
    DeliveryStatusDraft(id: widget.idBatch),
  ];

  @override
  void initState() {
    super.initState();
  }
}

2

Answers


  1. I think this is the problem:

    List<Widget> bodies = [
      DeliveryStatusDraft(id: widget.idBatch),
    ];
    

    You cannot access widget here. So what you could do is to create an empty array and initialize it in the initState.

    List<Widget> bodies = [];
    
    @override
    void initState() {
      super.initState();
      bodies = [
        DeliveryStatusDraft(id: widget.idBatch),
      ];
    }
    
    Login or Signup to reply.
  2. class SegmentationSchedule extends StatefulWidget {
      final int idBatch;
      const SegmentationSchedule({super.key, required this.idBatch});
      @override
      State<SegmentationSchedule> createState() => _SegmentationScheduleState();
    }
    

    In order to access member from constructor you should declare them as final so that you can use them as widget.idBatch. And use const keyword before your constructors.

    If a value is immutable, that value cannot be changed. To make a field of a class immutable, you can use either the const or final keyword. If you use the const keyword, you need to initialize the value in the field declaration

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search