'Pass value of variables to another widget Flutter

I'm trying to pass a value of a variable from a StatefulWiget to another StatefulWidget

class InputFieldCheckTick extends StatefulWidget {
double timbreFiscaleFournisseur = 0.000;
bool exoTVA = false;
.
.
.
value: isTicked,
              onChanged: (value) async {
                await setState(() {
                  isTicked = value;
                  if (isTicked == false) {
                    widget.exoTVA = false;
                  } else {
                    widget.exoTVA = true;
                  }
                });
.
.
.
value: isTicked,
              onChanged: (value) async {
                await setState(() {
                  isTicked = value;
                  if (isTicked == false) {
                    widget.exoTVA = false;
                  } else {
                    widget.exoTVA = true;
                  }
                });

and i'm trying to pass the values of exoTVA and timbreFiscaleFournisseur here :

setState(() {
                                  future = ajoutFournisseur(
                                      numeroFournisseur.text,
                                      addressFournisseur.text,
                                      matriculeFiscaleFournisseur.text,
                                      raisonSocialeFournisseur.text,
                                      paysFournisseur.text,
                                      villeFournisseur.text,
                                      InputFieldCheckTick()
                                          .timbreFiscaleFournisseur,
                                      InputFieldCheckTick().exoTVA);
                                });


Solution 1:[1]

I think you are creating an StatefulWidget which contains a Final property called exoTVA, something like:

class MyWidget extends StatefulWidget {
    final bool exoTVA;
    [...]

Because you are calling widget.exoTVA which refers to stateful related widget, and there is your mistake. You can't change widget.exoTVA for the way widgets are build. What you can do is to do this:

class _MyWidgetState extends State<MyWidget> {
    // Here you designate an instance value for your widget property
    bool? _exoTVA;
    // On initState you define from parent widget, I do this all the time
    @override
    initState((){
        super.initState();
        _exoTVA = widget.exoTVA;
    });
    [...] //Continue with the rest of your widget

Also when you call those changes in setState, change widget.exoTVA to _exoTVA, and to finish you can call that var like this:

setState(() {
                                  future = ajoutFournisseur(
                                      numeroFournisseur.text,
                                      addressFournisseur.text,
                                      matriculeFiscaleFournisseur.text,
                                      raisonSocialeFournisseur.text,
                                      paysFournisseur.text,
                                      villeFournisseur.text,
                                      InputFieldCheckTick()
                                          .timbreFiscaleFournisseur,
                                      _exoTVA);//Changed this line
                                });

For your reference, check out StatefulWidget class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sávio Batista