mercredi 25 octobre 2023

Using checkbox to determine next step in flutter app

I am using a checkbox to determine if a new user will be associated to an existing company or if they will need to add a company to be associated with.

This is the checkbox code:

Checkbox(
                  value: _chkbxValue,
                  onChanged: (bool? value) {
                    setState(() {
                      ref
                          .read(globalsNotifierProvider.notifier)
                          .updateAddCompany(value!);
                    });
                  },
                ),

In the onChanged: I set a global notifierprovider to the state of the checkbox which would be "true".

However, when I get down to the navigation code, the notifierprovider is back to "false" but I don't have any code that changes the notifierprovider.

Here is the navigation code. This code only gets executed when the "Save" button is pressed:

if (ref.read(globalsNotifierProvider).addCompany!) {
                        ref
                            .read(globalsNotifierProvider.notifier)
                            .updateAddCompany(false);
                        Navigator.of(context).pushReplacement(MaterialPageRoute(
                            builder: (context) => const CompanyScreen()));
                      } else {
                        Navigator.of(context).pushReplacement(MaterialPageRoute(
                            builder: (context) => MainScreen()));
                      }

I am thinking I am not understanding the execution flow of the code. This code is all in a ConsumerStatefulWidget class.

When I press the "Save" button some how the value of the notifierprovider gets changed to "false". How can I do this the right way?

Thanks.




Aucun commentaire:

Enregistrer un commentaire