Home > Net >  how to update the previous page from the current page child stateless widget after Navigator.pushNam
how to update the previous page from the current page child stateless widget after Navigator.pushNam

Time:03-13

I have a page (stateful widget) that loads a child widget, this child widget is a grid, and it's loads another child widget a single grid item details.

on single grid item details, there is InkWell, and when onTap I Navigate to a new page.

the grid widget and the single grid item are both stateless widgets

when I go back from the new page using the back button I would like to update the previews page.

for example from the single grid item i have:

Navigator.pushNamed(
          context,
          SingleMoviePage.routeName,
          arguments: movie,
        ).then((_){
          var pressed = Provider.of<MoviesListProvider>(context,listen: false).listPageButtonPressed;
          if (pressed) {
            updateChanges();
          }
        });

I can update the page by passing the updateChanges function to grid widget and from the grid widget to the page. but is there a better way to do it using provider ? or another way without the need to send the function from child to parent and from the parent to the grandparent ?

CodePudding user response:

First of all you can do so by putting your data source in a ChangeNotifier and when it's updated you can notify all widgets that depend on it.

Another fast solution is to access the statful widget's state using a GlobalKey like this:

GlobalKey<yourStateFulWidget> myKey = GlobalKey()

and then access and edite the data like:

 myKey.currentState!.someFunctionOrData();

note: to get a better understanding on how to do the first solution I recommend you to read the official example by the flutter team here:

https://docs.flutter.dev/development/data-and-backend/state-mgmt/simple

  • Related