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
I think this is the problem:
You cannot access
widget
here. So what you could do is to create an empty array and initialize it in theinitState
.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