Flutter: bloc, how to show an alert dialog

前端 未结 5 1692
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-19 19:01

I´m new in the bloc pattern and stream stuff. I want to show up an alert dialog when I press a button, but I can´t find a way to do it. Actually my code is:

Widg         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 19:52

    You can't show a dialog when build working. When you have new data, then you create a new widget. Probably better for you will be not using the stream in this case, but if it necessary you should use

    WidgetsBinding.instance.addPostFrameCallback((_) => yourFunction(context));

    or

    Future.microtask(() => showDialogFunction(context));

    in your if

    if (snapshot.hasData) { WidgetsBinding.instance.addPostFrameCallback((_) => showDialogFunction(context)); }

    This code will be launched after build method, so dialog will show immediately.

    Bloc function always return widget, so always return button() or different wiget when stream has data

提交回复
热议问题