【问题标题】:Flutter Upgrade: After updating latest version of flutter previous function not workingFlutter 升级:更新最新版本的 Flutter 后,以前的功能不起作用
【发布时间】:2018-09-25 12:49:48
【问题描述】:

我已经在flutter v0.5.1版本中尝试过这个功能,它工作正常,没有问题。更新到最新版本 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);
    }
  }

【问题讨论】:

  • 我找到了解决方案,错误的原因是我在使用此行稍微更改代码后在 initState() 方法中使用显示对话框 new Future.delayed(Duration.zero,() =>_onLoading() );工作正常
  • 请考虑将此作为答案发布(并随后接受),以便将来可能对其他人有用(cmets 可能随时删除)。

标签: dart flutter flutter-layout


【解决方案1】:

你需要来自

的上下文
@override Widget build(BuildContext context) {
....
....
makeRequest() 

Widget 构建后的方法粘贴...并像这样将参数(上下文)传递到括号中

makeRequest(context) 

再一次

_onLoading(context) 

并使用

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

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

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 2019-09-27
    • 2022-06-17
    • 1970-01-01
    • 2021-03-29
    • 2020-07-19
    • 2021-11-12
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多