为什么我的应用在Flutter中会不断执行已经执行过的函数?

huangapple go评论65阅读模式
英文:

why is my app keep executing a function that have already been executed in flutter

问题

我已经为穆斯林创建了一个应用程序,可以检查用户是否在他们的“solat”(礼拜)中进行了签到。我创建了一个函数,可以在用户每次打开应用程序时检查是否迟到。例如,现在是“zohor”(中午礼拜)的祈祷时间,如果他们在中午打开应用程序但没有在“subuh”(黎明礼拜)祈祷时间内签到,就会调用一个迟到的函数,将整数2保存在共享首选项中,键为'subuh'(黎明),其中2表示迟到,1表示早到(用户签到时保存),0是默认值。
一切都运行正常,但每次有迟到的祈祷时间时,迟到计数器就会不断增加,尽管该函数仅在首次检查祈祷时间时才调用。我的代码看起来很死板,因为我还是Flutter的新手。我已经在这个问题上卡了好几天了。

以下是检查每次用户打开应用程序的页面:

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;
    }
  }

其中一个函数用于存储我的祈祷签到信息:

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;
  }

增加迟到计数和增加迟到 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;
  }

希望这能帮助你解决问题。如果你有其他问题,欢迎提

英文:

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&lt;checkinlist&gt;(context, listen: false).getSubuh();
int zohor = Provider.of&lt;checkinlist&gt;(context, listen: false).getZohor();
int asar = Provider.of&lt;checkinlist&gt;(context, listen: false).getAsar();
int maghrib = Provider.of&lt;checkinlist&gt;(context, listen: false).getMaghrib();
int isyak = Provider.of&lt;checkinlist&gt;(context, listen: false).getIsyak();
bool checksubuh = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckSubuh();
bool checkzohor = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckZohor();
bool checkasar = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckAsar();
bool checkmaghrib = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckMaghrib();
bool checkisyak = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckIsyak();
switch(subuh){
case 0:
if (prayerTimes.currentPrayer().index &gt;= 1  ) {
if (checksubuh == false) {
setState(() {
Provider.of&lt;checkinlist&gt;(context, listen: false).SubuhChecked();
Provider.of&lt;checkinlist&gt;(context, listen: false).decreasehealth();
Provider.of&lt;checkinlist&gt;(context, listen: false).subuhlate();
});
}
}
break;
}
switch(zohor){
case 0:
if (prayerTimes.currentPrayer().index &gt;= 4  ) {
if (checkzohor == false ) {
setState(() {
Provider.of&lt;checkinlist&gt;(context, listen: false).zohorlate();
Provider.of&lt;checkinlist&gt;(context, listen: false).decreasehealth();
Provider.of&lt;checkinlist&gt;(context, listen: false).ZohorChecked();
});
}
}
break;
}
switch(asar){
case 0:
if (prayerTimes.currentPrayer().index &gt;= 5 ) {
if (checkasar == false ) {
setState(() {
Provider.of&lt;checkinlist&gt;(context, listen: false).asarlate();
Provider.of&lt;checkinlist&gt;(context, listen: false).decreasehealth();
Provider.of&lt;checkinlist&gt;(context, listen: false).AsarChecked();
});
}
}
break;
}
switch(maghrib){
case 0:
if (prayerTimes.currentPrayer().index &gt;= 6  ) {
if (checkmaghrib == false) {
setState(() {
Provider.of&lt;checkinlist&gt;(context, listen: false).maghriblate();
Provider.of&lt;checkinlist&gt;(context, listen: false).decreasehealth();
Provider.of&lt;checkinlist&gt;(context, listen: false).MaghribChecked();
});
}
}
break;
}
switch(isyak){
case 0:
if (DateTime.now().hour &gt;= 23  ) {
if (checkisyak == false) {
setState(() {
Provider.of&lt;checkinlist&gt;(context, listen: false).maghriblate();
Provider.of&lt;checkinlist&gt;(context, listen: false).decreasehealth();
Provider.of&lt;checkinlist&gt;(context, listen: false).MaghribChecked();
});
}
}
break;
default:
break;
}
}

One of the functions to stored my prayer check

Future&lt;void&gt; subuhcheckin() async {
final prefs = await SharedPreferences.getInstance();
subuh = 1;
prefs.setInt(&#39;subuh&#39;, subuh);
notifyListeners();
}
Future&lt;void&gt; subuhlate() async {
final prefs = await SharedPreferences.getInstance();
subuh = 2;
prefs.setInt(&#39;subuh&#39;, subuh);
increaseLateCounter();
increaseLateSubuh();
notifyListeners();
}
Future&lt;void&gt; loadSubuh()  async {
final prefs = await SharedPreferences.getInstance();
subuh = (prefs.getInt(&#39;subuh&#39;) ?? 0);
notifyListeners();
}
int getSubuh() {
loadSubuh();
return subuh;
}

increase late counter and increase late subuh

Future&lt;void&gt; increaseLateCounter()  async {
final prefs = await SharedPreferences.getInstance();
late = (prefs.getInt(&#39;late&#39;) ?? 0) + 1;
prefs.setInt(&#39;late&#39;, late);
notifyListeners();
}
Future&lt;void&gt; loadLate()  async {
final prefs = await SharedPreferences.getInstance();
late = (prefs.getInt(&#39;late&#39;) ?? 0);
notifyListeners();
}
int getLate() {
loadLate();
return late;
}
Future&lt;void&gt; increaseLateSubuh()  async {
final prefs = await SharedPreferences.getInstance();
latesubuh = (prefs.getInt(&#39;latesubuh&#39;) ?? 0) + 1;
prefs.setInt(&#39;latesubuh&#39;, latesubuh);
notifyListeners();
}
Future&lt;void&gt; loadLateSubuh()  async {
final prefs = await SharedPreferences.getInstance();
latesubuh = (prefs.getInt(&#39;latesubuh&#39;) ?? 0);
notifyListeners();
}
int getLateSubuh() {
loadLateSubuh();
return latesubuh;
}

答案1

得分: 0

问题出在

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

loadSubuh() 是一个异步函数。因此,当这个函数返回时,subuh 将为 0,因为 subuh 还没有被设置,因为 loadSubuh() 还没有完成。为了确保它被设置,你需要将这个函数也改成异步函数,并等待加载,像这样:

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

这会导致 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();

你应该这样做:

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();
英文:

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&lt;int&gt; 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&lt;checkinlist&gt;(context, listen: false).getSubuh();
int zohor = Provider.of&lt;checkinlist&gt;(context, listen: false).getZohor();
int asar = Provider.of&lt;checkinlist&gt;(context, listen: false).getAsar();
int maghrib = Provider.of&lt;checkinlist&gt;(context, listen: false).getMaghrib();
int isyak = Provider.of&lt;checkinlist&gt;(context, listen: false).getIsyak();
bool checksubuh = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckSubuh();
bool checkzohor = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckZohor();
bool checkasar = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckAsar();
bool checkmaghrib = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckMaghrib();
bool checkisyak = Provider.of&lt;checkinlist&gt;(context, listen: false).getCheckIsyak();

you should better do

    final provider = Provider.of&lt;checkinlist&gt;(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();
</details>

huangapple
  • 本文由 发表于 2023年1月9日 15:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定