Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading