Я пробовал эту функцию в версии Flutter v0.5.1, и она работает нормально, без проблем. После обновления до последней версии v0.8.4 я получаю исключение ниже.
ExcceptionDatainheritFromWidgetOfExactType(_LocalizationsScope) was called before ProfileScreen.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.
Typically references to to inherited widgets should occur in widget build() methods. Alternatively, initialization based on inherited widgets can be placed in the didChangeDependencies method, which is called after initState and whenever the dependencies change thereafter.
@override
void initState() {
super.initState();
makeRequest();
}
Future<DataModel> makeRequest() async {
_onLoading();
http.Response response = await http.get(Uri.encodeFull(getProfile),
headers: {"Accept": "application/json"});
/// json data calling.....
}
void _onLoading() {
if (loadCheck) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return new Dialog(
// progress dialog calling );
}
);
} else {
Navigator.pop(context);
}
}
Пожалуйста, подумайте о том, чтобы опубликовать это как ответ (и впоследствии принять его), чтобы он мог быть полезен другим в будущем (комментарии могут быть удалены в любое время).
Вам нужен контекст из
@override Widget build(BuildContext context) {
....
....
makeRequest()
вставьте метод после сборки виджета ... и передайте параметр (контекст) в круглые скобки, как это
makeRequest(context)
и снова
_onLoading(context)
и использовать
showDialog(
context: context, //The context from Widget Build(BuildContext context)...
Это обычная проблема, если вы используете Provider и InitState.
Я нашел решение, причину ошибки. Я использую диалоговое окно показа в методе initState () после небольшого изменения кода, используя эту строку new Future.delayed (Duration.zero, () => _ onLoading ()); работает нормально