admin管理员组文章数量:1405577
I am using Provider in my application. I have designed a simple question-answering mechanism. My code suddenly stopped working even though I didn’t change anything. Could it be that I have reached the maximum amount of data a Provider can hold, or is it a similar issue?
...
questionState = QuestionState.answered;
selectedOption = option;
notifyListeners(); // <=== THIS IS UPDATE UI
answerResponse = await ApiService.answerQuestion(
startResponse!.gameID,
activeQuestionNEW!.id,
option?.id,
startResponse!.levelConfig.timePerQuestion - remainingTime,
);
await Future.delayed(const Duration(seconds: 1));
_checkQuestionAnswer();
answerList.add(_createAnswerObject(option: option));
LogService.logValue(answerResponse, valueName: 'answerResponse');
notifyListeners(); // <=== THIS IS NOT UPDATE UI
showResultAnimation();
_playAnswerSound();
...
I am using Provider in my application. I have designed a simple question-answering mechanism. My code suddenly stopped working even though I didn’t change anything. Could it be that I have reached the maximum amount of data a Provider can hold, or is it a similar issue?
...
questionState = QuestionState.answered;
selectedOption = option;
notifyListeners(); // <=== THIS IS UPDATE UI
answerResponse = await ApiService.answerQuestion(
startResponse!.gameID,
activeQuestionNEW!.id,
option?.id,
startResponse!.levelConfig.timePerQuestion - remainingTime,
);
await Future.delayed(const Duration(seconds: 1));
_checkQuestionAnswer();
answerList.add(_createAnswerObject(option: option));
LogService.logValue(answerResponse, valueName: 'answerResponse');
notifyListeners(); // <=== THIS IS NOT UPDATE UI
showResultAnimation();
_playAnswerSound();
...
Share
Improve this question
asked Mar 23 at 11:39
Feyiz Oktay AydırFeyiz Oktay Aydır
998 bronze badges
1
|
1 Answer
Reset to default 1It’s possible that your second notifyListeners() is not being executed due to an error or an issue in the state management flow. To debug this, follow these steps:
Run your app in debug mode and add breakpoints near the second notifyListeners().
Else wrap your logic in a try-catch block to see if any error occurs before reaching notifyListeners().
try {
answerResponse = await ApiService.answerQuestion(
startResponse!.gameID,
activeQuestionNEW!.id,
option?.id,
startResponse!.levelConfig.timePerQuestion - remainingTime,
);
await Future.delayed(const Duration(seconds: 1));
_checkQuestionAnswer();
answerList.add(_createAnswerObject(option: option));
LogService.logValue(answerResponse, valueName: 'answerResponse');
notifyListeners(); // <=== Check if this executes
} catch (e, stackTrace) {
debugPrint("Error before notifyListeners: $e");
debugPrint(stackTrace.toString());
}
- Calling notifyListeners() updates only the widgets that are actively listening to the provider at that moment. Check if your Consumer or Selector widget is properly wrapped around the widget that should rebuild.
Consumer<MyProvider>(
builder: (context, provider, child) {
return Text(provider.someValue);
},
);
If still not updating? Share more code which can help to reproduce the issue and understanding the problem ✌️
本文标签: flutterProvider notifyListeners() is not update ConsumersStack Overflow
版权声明:本文标题:flutter - Provider notifyListeners() is not update Consumers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744288261a2598987.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Provider
used in the UI ? because if you use more than oneConsumer
for different Providers on one UI component it will need to combine the Providers method together so they can work – Mahmoud Al-shehyby Commented Mar 23 at 17:32