Home > Net >  Reload previous page when Navigator.of().pop() called in next page
Reload previous page when Navigator.of().pop() called in next page

Time:10-25

This is code to show next page from profile page. In this edit page, there are several buttons to edit user information. When users finished to edit, Navigator.of(context).pop() called and profile page appears again.

In profile page:

String userName = "none"; // public string
........
Text(userName),
IconButton(icon: Icons.edit, onPressed: () => Navigator.of(context).pushNamed("/edit"))

In edit page:

IconButton(icon: Icons.edit, onPressed: () => username = "ABCD")

and when user finished editing, push a back button in appbar. this button is standard of Navigator not unique widget.


These code can't change Text(userName) when back from edit page, just show old string.

So how can I call this profile page again? Or reload only userName string?

CodePudding user response:

You can use a state management package like RiverPod and update the specific widget. Update that specific widget by using Scoped Provider and listening to that specific value.

More about RiverPod

CodePudding user response:

As Yash presented, it worked with .then((value) => setState(() {})).

profile page:

String userName = "none";
.....
Text(userName),
IconButton(icon: Icons.edit, onPressed: () => Navigator.of(context).pushNamed("/edit").then((value) => setState(() {})))

edit page:

setState( () { userName = "ABCDEFG"; })
  • Related