Flutter - Always execute a function when the page appears

后端 未结 5 1115
暗喜
暗喜 2021-01-20 03:56

How could I make the name() function run whenever the Page1 page appeared?

In the code below before going to Page2 I execute t

5条回答
  •  爱一瞬间的悲伤
    2021-01-20 04:56

    One way of doing this is to use the .whenComplete() method on the Navigator widget.

    Suppose you are going to the second page from the first page. Here you have to pass the functionThatSetsTheState as a pointer to the navigation part of your code.

    The function looks like this and should be in a Stateful Widget.

    void functionThatSetsTheState(){
     setState(() {});
    }
    

    Your navigation code for OnPressed, OnTap, OnLongPress, etc.

    Navigator.of(context).push(MaterialPageRoute(builder: (BuildContext context) => SecondPage())).whenComplete(() => {functionThatSetsTheState()});
    

提交回复
热议问题