Looking up a deactivated widget's ancestor is unsafe

前端 未结 5 2073
鱼传尺愫
鱼传尺愫 2020-12-29 03:46

I am new in Flutter and I am trying receive data with a Dialog. When a click in textField the error of image2 appear...

5条回答
  •  旧时难觅i
    2020-12-29 03:54

    I got the same error when attempting to open a dialog and I found a solution here: github flutter issues. Specifically, I followed the poster's recommendation, which was to create a GlobalKey and associate it with the Scaffold widget, and use the context from that key when creating the dialog. In my case, I have a globally accessible object which holds the GlobalKey:

    MyGlobals myGlobals = MyGlobals();
    class MyGlobals {
      GlobalKey _scaffoldKey;
      MyGlobals() {
        _scaffoldKey = GlobalKey();
      }
      GlobalKey get scaffoldKey => _scaffoldKey;
    }
    

    In the Scaffold widget constructor call:

    Scaffold(
      appBar: ...,
      body: ...,
      drawer: ...,
      key: myGlobals.scaffoldKey,
    )
    

    And in the showDialog call:

    showDialog(
      barrierDismissible: ...,
      builder: ...,
      context: myGlobals.scaffoldKey.currentContext,
    );
    

提交回复
热议问题