Home > database >  Flutter - Navigator.pop() - rebuild origin screen with setState()
Flutter - Navigator.pop() - rebuild origin screen with setState()

Time:10-23

How can I rebuild with my "HomeScreen" with setState after I've manipulated some displayed data (in HomeScreen) in a SecondScreen? I'm using Navigator.pop() but it isn't showing the changes.

SecondScreen:

      onPressed: () {
        setState(
          () {
            holidays[widget.index].start = test;
          },
        );
        Navigator.pop(
          context,
        );
      },

On my homescreen I have a list of "holidays". In the Holiday class I have a button....

                   GestureDetector(
                         onTap: () async {
                              await Navigator.push(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => EditScreen(
                                      start: start, end: end, index: index),
                                ),
                              );
                            },

CodePudding user response:

Did you try calling setState() after Navigator.push() ?

CodePudding user response:

OK so I had to create a callback function in my main file which is basically just setState(), pass the function when creating an instance of Holiday, and then run the callback function after await Navigator.push()

Callback function:

  void rebuild() {
    print("test");
    setState(() {});
  }

Navigator.push() in my Holiday class

                    onTap: () async {
                      await Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => EditScreen(
                              start: start,
                              end: end,
                              index: index,
                              ),
                        ),
                      );
                      rebuild!();
                    },
  • Related