Force Flutter navigator to reload state when popping

前端 未结 14 2195
情书的邮戳
情书的邮戳 2020-11-28 22:54

I have one StatefulWidget in Flutter with button, which navigates me to another StatefulWidget using Navigator.push(). On second widge

14条回答
  •  孤独总比滥情好
    2020-11-28 23:20

    Needed to force rebuild of one of my stateless widgets. Did't want to use stateful. Came up with this solution:

    await Navigator.of(context).pushNamed(...);
    ModalRoute.of(enclosingWidgetContext);
    

    Note that context and enclosingWidgetContext could be the same or different contexts. If, for example, you push from inside StreamBuilder, they would be different.

    We don't do anything here with ModalRoute. The act of subscribing alone is enough to force rebuild.

提交回复
热议问题