AlertDialog without context in Flutter

前端 未结 4 1905
遥遥无期
遥遥无期 2021-02-02 12:50

I want to show an AlertDialog when a http get fails. The function showDialog (https://api.flutter.dev/flutter/material/showDialog.html) has the parameter \"@required BuildContex

4条回答
  •  春和景丽
    2021-02-02 13:06

    So you need a BuildContext to create a dialog but you don't have access to it. That's a common problem, you can refer to this StackOverflow question for one of the approaches to solve it (create a static dialog and show it from wherever you need).

    Another approach you may consider is to pass the context when creating an async method or object as an argument. Make sure you null it when you're done.

    Or you can make a flag (boolean) which becomes 'true' under a certain condition, and in one of the build() methods you always check that flag, and if it's 'true' - do your thing (show a dialog for instance).

提交回复
热议问题