how to use a data transfer from the parent widget?

Here is the child class that receives from its parent the index data that identifies the box to modify

class InformationPatient extends StatefulWidget {
  final Patients patients;
  final int index;

  const InformationPatient(this.patients, this.index, {Key? key})
      : super(key: key);

  @override
  State<InformationPatient> createState() => _InformationPatientState();
}

class _InformationPatientState extends State<InformationPatient> {

  final int indexitem = 0;

  late Box<Patients> boxPatient;

  @override
  void initState() {
    super.initState();
    boxPatient = Hive.box('Patient');
  }

  void _addNote(String newtitle, String newnote, String newconclusion) {
    final newNOTE = Patients(
      listOfNotes: [
        ListNote(title: newtitle, note: newnote, conclusion: newconclusion)
      ],
    );
    boxPatient.put(indexitem, newNOTE);
    Navigator.of(context).pop();
  }

This way works but I don’t have the index of the parent, I give it to him

final int indexitem = 0; <--- Works perfectly with my function patientbox.put() it receives the index
boxPatient.put(indexitem, newNOTE);

I would like to give indexbox the value of index but I don’t know how to do it or if there is another way

final int indexitem = ???  <---- add the value of index
boxPatient.put(indexitem, newNOTE);

obviously I can’t use index directly in the function boxPatient.put()

Thank you very much for your help

>Solution :

To read variables from the widget inside the state, you can simply refer to widget.<variable>. So in your example

boxPatient.put(widget.index, newNOTE);

Leave a Reply