简体   繁体   English

Flutter 升级:更新最新版本的 Flutter 之前的功能不起作用

[英]Flutter Upgrade: After updating latest version of flutter previous function not working

I have tried this function in flutter version v0.5.1 and it is working fine, no issue.我已经在 flutter 版本 v0.5.1 中尝试过这个功能,它工作正常,没有问题。 After I have updated to the latest version v0.8.4 I get the exception below.更新到最新版本 v0.8.4 后,出现以下异常。

ExcceptionDatainheritFromWidgetOfExactType(_LocalizationsScope) was called before ProfileScreen.initState() completed. ExceptionDatainheritFromWidgetOfExactType(_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);
    }
  }

You need the context from你需要上下文

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

method paste after Widget build... and pass parameter (context) into parenthesis like this在 Widget 构建之后粘贴方法...并将参数(上下文)传递到这样的括号中

makeRequest(context) 

and again然后再次

_onLoading(context) 

and use并使用

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

It is a common problem if you use Provider and InitState.如果使用 Provider 和 InitState,这是一个常见问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM