Home > Software design >  why is my app keep executing a function that have already been executed in flutter
why is my app keep executing a function that have already been executed in flutter

Time:01-09

Ive created an app for muslims that can check whether the user have check in their solat or not. I created a function that can check if the user is late everytime they open the app. For example, now is zohor prayer time, and if they open the app during zohor but did not check in during subuh prayer time, a late function is called which saves the integer 2 in a shared preferences with 'subuh'. ( 2 is equals to late, 1 is equal early and saved when the user check in, 0 is default) Everything works fine but everytime there is a late prayer time, the late counter keeps increasing eventhough the function only calls when the first time a prayer time is check. My code looks very hard coded because im still new to flutter. Have been stuck in this problem for days now

below is the page which checks the function everytime user opens the app

void latecheck(){
    int subuh = Provider.of<checkinlist>(context, listen: false).getSubuh();
    int zohor = Provider.of<checkinlist>(context, listen: false).getZohor();
    int asar = Provider.of<checkinlist>(context, listen: false).getAsar();
    int maghrib = Provider.of<checkinlist>(context, listen: false).getMaghrib();
    int isyak = Provider.of<checkinlist>(context, listen: false).getIsyak();
    bool checksubuh = Provider.of<checkinlist>(context, listen: false).getCheckSubuh();
    bool checkzohor = Provider.of<checkinlist>(context, listen: false).getCheckZohor();
    bool checkasar = Provider.of<checkinlist>(context, listen: false).getCheckAsar();
    bool checkmaghrib = Provider.of<checkinlist>(context, listen: false).getCheckMaghrib();
    bool checkisyak = Provider.of<checkinlist>(context, listen: false).getCheckIsyak();

    switch(subuh){
      case 0:
        if (prayerTimes.currentPrayer().index >= 1  ) {
          if (checksubuh == false) {
           setState(() {
             Provider.of<checkinlist>(context, listen: false).SubuhChecked();
             Provider.of<checkinlist>(context, listen: false).decreasehealth();
             Provider.of<checkinlist>(context, listen: false).subuhlate();
           });
          }
        }
        break;
    }
    switch(zohor){
      case 0:
        if (prayerTimes.currentPrayer().index >= 4  ) {
          if (checkzohor == false ) {
            setState(() {
              Provider.of<checkinlist>(context, listen: false).zohorlate();
              Provider.of<checkinlist>(context, listen: false).decreasehealth();
              Provider.of<checkinlist>(context, listen: false).ZohorChecked();
            });
          }
        }
        break;
        }
    switch(asar){
      case 0:
        if (prayerTimes.currentPrayer().index >= 5 ) {
          if (checkasar == false ) {
            setState(() {
              Provider.of<checkinlist>(context, listen: false).asarlate();
              Provider.of<checkinlist>(context, listen: false).decreasehealth();
              Provider.of<checkinlist>(context, listen: false).AsarChecked();
            });
          }
        }
        break;
    }
    switch(maghrib){
      case 0:
        if (prayerTimes.currentPrayer().index >= 6  ) {
          if (checkmaghrib == false) {
            setState(() {
              Provider.of<checkinlist>(context, listen: false).maghriblate();
              Provider.of<checkinlist>(context, listen: false).decreasehealth();
              Provider.of<checkinlist>(context, listen: false).MaghribChecked();
            });
          }
        }
        break;
    }
    switch(isyak){
      case 0:
        if (DateTime.now().hour >= 23  ) {
          if (checkisyak == false) {
            setState(() {
              Provider.of<checkinlist>(context, listen: false).maghriblate();
              Provider.of<checkinlist>(context, listen: false).decreasehealth();
              Provider.of<checkinlist>(context, listen: false).MaghribChecked();
            });
          }
        }
        break;

      default:
        break;
    }

  }

One of the functions to stored my prayer check

Future<void> subuhcheckin() async {
    final prefs = await SharedPreferences.getInstance();
    subuh = 1;
    prefs.setInt('subuh', subuh);
    notifyListeners();
  }

  Future<void> subuhlate() async {
    final prefs = await SharedPreferences.getInstance();
    subuh = 2;
    prefs.setInt('subuh', subuh);
    increaseLateCounter();
    increaseLateSubuh();
    notifyListeners();
  }

  Future<void> loadSubuh()  async {
    final prefs = await SharedPreferences.getInstance();
    subuh = (prefs.getInt('subuh') ?? 0);
    notifyListeners();
  }

  int getSubuh() {
    loadSubuh();
    return subuh;
  }

increase late counter and increase late subuh

Future<void> increaseLateCounter()  async {
    final prefs = await SharedPreferences.getInstance();
    late = (prefs.getInt('late') ?? 0)   1;
    prefs.setInt('late', late);
    notifyListeners();
  }


  Future<void> loadLate()  async {
    final prefs = await SharedPreferences.getInstance();
    late = (prefs.getInt('late') ?? 0);
    notifyListeners();
  }

  int getLate() {
    loadLate();
    return late;
  }

  Future<void> increaseLateSubuh()  async {
    final prefs = await SharedPreferences.getInstance();
    latesubuh = (prefs.getInt('latesubuh') ?? 0)   1;
    prefs.setInt('latesubuh', latesubuh);
    notifyListeners();
  }


  Future<void> loadLateSubuh()  async {
    final prefs = await SharedPreferences.getInstance();
    latesubuh = (prefs.getInt('latesubuh') ?? 0);
    notifyListeners();
  }

  int getLateSubuh() {
    loadLateSubuh();
    return latesubuh;
  }

CodePudding user response:

The problem is with

int getSubuh() {
  loadSubuh();
  return subuh;
}

loadSubuh() is an async function. Therefore subuh will be 0 when this function returns because subuh is not set yet because loadSubuh() is not finished yet. To make sure it's set you need to make this function async as well and await the load, so like

Future<int> getSubuh() async {
  await loadSubuh();
  return subuh;
}

This will cause that latecheck() also needs to be async and you need to await there also accordingly.

Another tip unrelated to your error is that you should minimize the number of same function calls if you can. For example instead of

    int subuh = Provider.of<checkinlist>(context, listen: false).getSubuh();
    int zohor = Provider.of<checkinlist>(context, listen: false).getZohor();
    int asar = Provider.of<checkinlist>(context, listen: false).getAsar();
    int maghrib = Provider.of<checkinlist>(context, listen: false).getMaghrib();
    int isyak = Provider.of<checkinlist>(context, listen: false).getIsyak();
    bool checksubuh = Provider.of<checkinlist>(context, listen: false).getCheckSubuh();
    bool checkzohor = Provider.of<checkinlist>(context, listen: false).getCheckZohor();
    bool checkasar = Provider.of<checkinlist>(context, listen: false).getCheckAsar();
    bool checkmaghrib = Provider.of<checkinlist>(context, listen: false).getCheckMaghrib();
    bool checkisyak = Provider.of<checkinlist>(context, listen: false).getCheckIsyak();

you should better do

    final provider = Provider.of<checkinlist>(context, listen: false);
    int subuh = provider.getSubuh();
    int zohor = provider.getZohor();
    int asar = provider.getAsar();
    int maghrib = provider.getMaghrib();
    int isyak = provider.getIsyak();
    bool checksubuh = provider.getCheckSubuh();
    bool checkzohor = provider.getCheckZohor();
    bool checkasar = provider.getCheckAsar();
    bool checkmaghrib = provider.getCheckMaghrib();
    bool checkisyak = provider.getCheckIsyak();
  • Related