本文介绍了未处理的异常:在_ScreenState.initState()完成之前调用了InheritFromFromWidgetOfExactType(_LocalizationsScope)或herineFromElement().的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在调用初始方法以使用initState从API加载数据.但这导致我出错.这是错误:
I am calling initial method to load data from API using initState. But it is resulting me an error. Here is error:
Unhandled Exception: inheritFromWidgetOfExactType(_LocalizationsScope) or inheritFromElement() was called before _ScreenState.initState() completed.
When an inherited widget changes, for example if the value of Theme.of() changes, its dependent widgets are rebuilt. If the dependent widget's reference to the inherited widget is in a constructor or an initState() method, then the rebuilt dependent widget will not reflect the changes in the inherited widget.
我的代码是:
@override
void initState() {
super.initState();
this._getCategories();
}
void _getCategories() async {
AppRoutes.showLoader(context);
Map<String, dynamic> data = await apiPostCall(
apiName: API.addUser,
context: context,
parameterData: null,
showAlert: false,
);
if(data.isNotEmpty){
AppRoutes.dismissLoader(context);
print(data);
}else {
AppRoutes.dismissLoader(context);
}
}
推荐答案
您需要在 initState
完成后调用 _getCategories
.
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () {
this._getCategories();
});
// Could do this in one line: Future.delayed(Duration.zero, this._getCategories);
}
此外,您可以使用 addPostFrameCallback 就像在另一个答案中显示的一样.
Also, you could do this on a different way, using addPostFrameCallback like showed in another answers.
这篇关于未处理的异常:在_ScreenState.initState()完成之前调用了InheritFromFromWidgetOfExactType(_LocalizationsScope)或herineFromElement().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!