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

bool value always returns true flutter

I am having trouble with the bool value. My bool value returns true only.
I have five buttons which set the state for the bool and time for fasting.

 ElevatedButton(
                        onPressed: () async {
                          final SharedPreferences prefs = await _prefs;
                          playLocalAsset(0);
                          if((isFasting = true) && (widget.isFasting = true)) {
                            showError();
                            removeHours(fastDurs);
                            setState(() {
                              isFasting = false;
                              widget.isFasting = false;
                            });
                            //showAlerts();
                          } else {
                            setState(() {
                              isFasting = true;
                              widget.isFasting = true;
                              fastDuration = 16;
                              sixteen = 16;
                              eighteen = null;
                              twenty= null;
                              twentytwo = null;
                              twentyfour = null;
                              widget.fastDuration= 16;
                              endTime = startTime.add(Duration(hours: fastDuration!));
                              endTimeS = DateFormat('y/M/d, hh:mma').format(endTime);
                              _textLine = prefs
                                  .setString('formattedDate', endTimeS)
                                  .then((value) => (prefs
                                  .getString('formattedDate') ??
                                  Languages.of(context)!.setYourFastTime +'\n'+ startTimeS));

                              textDate = prefs.setString('eatTime', Languages.of(context)!.youWillEatAt)
                                  .then((value) => (prefs.getString('eatTime') ?? ''));
                              showMessage(Languages.of(context)!.yourFastTimeIsSet);
                              updateHours(16);
                            });
                          }
                          },
                        child: Text('16:8', style: Constants.textTitleStyle),
                        style: Constants.buttonStyle),
                    const SizedBox(width:170),
                    ElevatedButton(
                        onPressed: () async {
                          final SharedPreferences prefs = await _prefs;
                          playLocalAsset(0);
                          if ((isFasting = true) && (widget.isFasting = true)) {
                            showError();
                            removeHours(fastDurs);
                            setState(() {
                              isFasting = false;
                              widget.isFasting = false;
                            });
                            //showAlerts();
                          } else {
                            setState(() {
                              isFasting = true;
                              widget.isFasting = true;
                              fastDuration = 18;
                              eighteen = 18;
                              sixteen = null;
                              twenty= null;
                              twentytwo = null;
                              twentyfour = null;
    widget.fastDuration = 18;
    endTime = startTime.add(Duration(hours: fastDuration!));
    endTimeS = DateFormat('y/M/d, hh:mma').format(endTime);
    _textLine = prefs
        .setString('formattedDate', endTimeS)
        .then((value) =>
    (prefs
        .getString('formattedDate') ??
        Languages.of(context)!.setYourFastTime + '\n' + startTimeS));
    textDate =
        prefs.setString('eatTime', Languages.of(context)!.youWillEatAt)
            .then((value) => (prefs.getString('eatTime') ?? ''));
    showMessage(Languages.of(context)!.yourFastTimeIsSet);
    updateHours(18);
  });
                          }
                          showNotificationOneTime(context,
                              startTime.add(const Duration(hours: 14)).hour,
                              startTime.add(const Duration(minutes: 0)).minute);
                          showNotif(context,
                              endTime.hour, endTime.minute);},
                        child: Text('18:6', style: Constants.textTitleStyle), 
                        style: Constants.buttonStyle
                    ),

i declare my isFasting bool as nullable, then in init I check for the time, and if it is before the endTime then bool isFasting = true. As such:

@override
void initState() {
super.initState();

String startTimeS = DateFormat('y/M/d, hh:mma').format(startTime);
String endTimeS = DateFormat('y/M/d, hh:mma').format(endTime);

_textLine = _prefs.then((SharedPreferences prefs) {
  String? fastEndTime = prefs.getString('formattedDate');
  if ((fastEndTime != null) && (endTime
          .isAfter(DateFormat('y/M/d, hh:mma').parse(fastEndTime)))) {
    setState(() {
      isFasting = false;
      widget.isFasting = false;
    });
    return Languages.of(context)!.yourFastTimeFinished +"\n"+ endTimeS;
  }
  if (isFasting = false) {
    return Languages.of(context)!.setYourFastTime +"\n"+ startTimeS;
  }
  if ((fastEndTime != null) && (endTime
      .isBefore(DateFormat('y/M/d, hh:mma').parse(fastEndTime)))){
    setState(() {
      isFasting = true;
      widget.isFasting = true;
    });
  }
    return fastEndTime ??
        Languages.of(context)!.setYourFastTime + "\n" + startTimeS;
});

textDate = _prefs.then((SharedPreferences prefs) {
  String? stillFastingTime = prefs.getString('eatTime');
  String? fastEndTime = prefs.getString('formattedDate');
  if ((fastEndTime != null) && (endTime
      .isBefore(DateFormat('y/M/d, hh:mma').parse(fastEndTime)))) {
    setState(() {
      isFasting = true;
      widget.isFasting = true;
    });
    return Languages.of(context)!.youWillEatAt;
  }
  return stillFastingTime ?? '';
});
}

so, if isFasting is true then fast is cancelled and isFasting set to false. then clicking on a diff button should set state as shown in else condition. However, my isFasting is always true. it does not turn off… And it does not remove the previous hours, and it does not change the string of the text according to state. How do I make that bool value work properly? Thank you for your help!

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

>Solution :

use == instead of = when checking for equality:

if (isFasting = false) to if (isFasting == false)

and

if((isFasting = true) && (widget.isFasting = true)) to if(isFasting == true && widget.isFasting == true) also the braces here can be removed.

!= is correct here

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