Flutter升级:在更新了最新版本的Flutter之前的功能后,无法使用

问题描述 投票:1回答:1

我已经在v0.5.1的flutter版本中尝试了此功能,并且工作正常,没有问题。更新到最新版本v0.8.4后,出现以下异常。

ExcceptionDatainheritFromWidgetOfExactType(_LocalizationsScope)原为在ProfileScreen.initState()完成之前调用。

        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);
    }
  }
dart flutter flutter-layout
1个回答
-1
投票

您需要上下文来自

@override Widget build(BuildContext context) {
....
....
makeRequest() 

在小部件构建后粘贴方法...,然后像这样将参数(上下文)传递到括号中

makeRequest(context) 

再次]

_onLoading(context) 

和使用

showDialog(
        context: context, //The context from Widget Build(BuildContext context)...

如果使用Provider和InitState,这是一个常见问题。

© www.soinside.com 2019 - 2024. All rights reserved.