'State is changed to true returns to false

I created a CheckBox list, where I get the verified value of the endpoint as false, but when changing the setstate it changes to true but then returns to false, i think FuturueBuilder rebuild CheckLists when setstate is called... But i have no idea how fix it.

e.checked is parsed ...snapshot.data!.secoes![i].checklists! .map<Widget>((e) attribute coming from the model that it receives through the api, for each checkListTile there must be a value to change in onChanged.

SingleChildScrollView newAppointmentBody() {
return SingleChildScrollView(
  child: FutureBuilder<AppointmentRepository>(
    future: _newReport(),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (snapshot.hasData) {
        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 30),
          child: Form(
            key: _formKey,
            child: SizedBox(
              child: Card(
                elevation: 3,
                child: Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 10, vertical: 30),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          const Text(
                            'Emissor: ',
                            style: TextStyle(
                              fontSize: 16,
                            ),
                          ),
                          Text(
                            '${widget.employee} ',
                            style: const TextStyle(
                                fontSize: 16, fontWeight: FontWeight.w600),
                          )
                        ],
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Setor do Emissor *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _status,
                          items: dropdownSetorEmissor,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectedSetor = chooseValue!;
                                _status = chooseValue;
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Turno do Emissor *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _emissor,
                          items: dropdownTurnoEmissor,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectEmissor = chooseValue!;
                                _emissor = chooseValue;
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Setor do risco / ocorrência *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _risco,
                          items: dropdownSetorRisco,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectRisco = chooseValue!;
                                _risco = chooseValue;
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Local / Equipamento *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      TextFormField(
                        controller: _controllerLocalEquip,
                        maxLines: 3,
                        focusNode: focus,
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Fonte *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _fonte,
                          items: dropdownFonte,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectFonte = chooseValue!;
                                _fonte = chooseValue;
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Caracterização *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _caracterizacao,
                          items: dropdownCaracterizacao,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectCaracterizacao = chooseValue!;
                                _caracterizacao = chooseValue;
                                if (chooseValue
                                    .contains('Segurança do Trabalho')) {
                                  isCheckList = true;
                                } else {
                                  isCheckList ^= isCheckList;
                                }
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Prioridade *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _prioridade,
                          items: dropdownPrioridade,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectPrioridade = chooseValue!;
                                _prioridade = chooseValue;
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: const [
                          Text(
                            'Reincidência *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          ),
                        ],
                      ),
                      DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: const Text(
                            'Moderado?',
                            style: TextStyle(
                                fontSize: 20, color: Colors.black54),
                          ),
                          isExpanded: true,
                          value: _reincidencia,
                          items: dropdownReincidencia,
                          onChanged: (String? chooseValue) {
                            setState(
                              () {
                                selectReincidencia = chooseValue!;
                                _reincidencia = chooseValue;
                              },
                            );
                          },
                        ),
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          GestureDetector(
                            child: const Text(
                              'Dicas',
                              style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.w700),
                            ),
                            onTap: () => alertDialogShowTips(),
                          ),
                        ],
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      const Divider(
                        thickness: 2,
                      ),
                      const SizedBox(
                        height: 15,
                      ),
                      Visibility(
                        child: ListView.builder(
                          physics: const NeverScrollableScrollPhysics(),
                          shrinkWrap: true,
                          itemCount: 6,
                          itemBuilder: (context, i) {
                            snapshot.data!.secoes![i].checklists!.map(
                              (e) => setState(() => isChecked = e.checked),
                            );
                            return Column(
                              children: <Widget>[
                                Container(
                                  width: double.infinity,
                                  padding: const EdgeInsets.only(left: 15),
                                  color: Colors.grey[200],
                                  child: Row(
                                    children: [
                                      Text(
                                        '${snapshot.data!.secoes![i].nome}',
                                        style: const TextStyle(
                                            fontSize: 16,
                                            fontWeight: FontWeight.bold),
                                      ),
                                      const SizedBox(
                                        height: 30,
                                      ),
                                    ],
                                  ),
                                ),
                                ...snapshot.data!.secoes![i].checklists!
                                    .map(
                                  (e) {
                                    return CheckboxListTile(
                                      title: Text('${e.quesito}'),
                                      value: e.checked,
                                      onChanged: (value) {
                                        setState(() {
                                          e.checked ^= value!;
                                          print(e.checked);
                                        });
                                      },
                                    );
                                  },
                                ).toList(),
                              ],
                            );
                          },
                        ),
                        visible: isCheckList,
                      ),
                      SizedBox(
                        height: 50,
                        width: 250,
                        child: ElevatedButton(
                          style: ButtonStyle(
                            shape: MaterialStateProperty.all<
                                RoundedRectangleBorder>(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(12.0),
                                side: const BorderSide(
                                    color: Colors.transparent),
                              ),
                            ),
                            backgroundColor:
                                MaterialStateProperty.all(Colors.black),
                          ),
                          onPressed: () => _getFromCamera(),
                          child: const Text(
                            'Adicionar foto',
                            style: TextStyle(fontSize: 22),
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      SizedBox(
                        height: 50,
                        width: 250,
                        child: ElevatedButton(
                          style: ButtonStyle(
                            shape: MaterialStateProperty.all<
                                RoundedRectangleBorder>(
                              RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(12.0),
                                side: const BorderSide(
                                    color: Colors.transparent),
                              ),
                            ),
                            backgroundColor:
                                MaterialStateProperty.all(Colors.black),
                          ),
                          onPressed: () => _getFromGalery(),
                          child: const Text(
                            'Adicionar imagem',
                            style: TextStyle(fontSize: 22),
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      image != null
                          ? AnimatedOpacity(
                              opacity: image != null ? 1 : 0,
                              duration: const Duration(milliseconds: 500),
                              child: AspectRatio(
                                aspectRatio: 2 / 3,
                                child: image != null
                                    ? Image.file(
                                        _image,
                                        scale: 1,
                                      )
                                    : null,
                              ),
                            )
                          : const SizedBox(),
                      const SizedBox(
                        height: 30,
                      ),
                      Row(
                        children: const [
                          Text(
                            'Descrição da Anomalia *',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          )
                        ],
                      ),
                      TextFormField(
                        maxLines: 5,
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      Row(
                        children: const [
                          Text(
                            'Ação Imediata',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          )
                        ],
                      ),
                      TextFormField(
                        maxLines: 5,
                      ),
                      const SizedBox(
                        height: 30,
                      ),
                      Row(
                        children: const [
                          Text(
                            'Ação Sugerida',
                            style:
                                TextStyle(color: Colors.grey, fontSize: 16),
                          )
                        ],
                      ),
                      TextFormField(
                        maxLines: 5,
                      ),
                      const SizedBox(
                        height: 45,
                      ),
                      SizedBox(
                        height: 50,
                        width: double.infinity,
                        child: ElevatedButton(
                          onPressed: () {
                            _newReport();
                          },
                          child: const Text('SALVAR'),
                          style: ButtonStyle(
                            backgroundColor:
                                MaterialStateProperty.all(Colors.green),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      }
      return Container();
    },
  ),
);

}



Sources

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

Source: Stack Overflow

Solution Source