First off, I have code like this:
// Assume First, Second, and Third exist.
class FirstState extends State<First> {
@override
Widget build(BuildContext context) {
return getMenuButton(
onPressed: () async => {
await Navigator.push(
context,
CustomMaterialPageRoute(
builder: (context) => Second()),
),
print("Back to first"),
});
}
}
class SecondState extends State<Second> {
@override
Widget build(BuildContext context) {
return getMenuButton(
onPressed: () async => {
await Navigator.pushReplacement(
context,
CustomMaterialPageRoute(
builder: (context) => Third()),
),
});
}
}
class ThirdState extends State<Third> {
@override
Widget build(BuildContext context) {
return getMenuButton(
onPressed: () async => {
await Navigator.pop()
});
}
}
Note that First does push
, Second does pushReplacement
, and Third does pop
.
What happens:
- Press button on First.
- Press button on Second.
- You see "Back to First".
What I want to happen:
- Press button on First.
- Press button on Second.
- Press button on Third.
- You see "Back to First".
In short, the problem is pushReplacement
causes the await Navigator.push
in First to return immediately (as per the docs), whereas I want it to work as if I just did push
, where it will keep waiting until it pops.
The reason I want this is because I want to pass data back from Third to First, but if I use pushReplacement I cannot do this. As a workaround, I would like to await the push in First and then reload some data in First, but I cannot find a way to make First wait for Third to pop.
Perhaps if I could control what pushReplacement
returns, I could make it return a key to Third and then await that? This seems pretty janky though.
tl;dr: How do I wait for a child navigation route when I use pushReplacement
in the child, breaking the "await connection" from parent to grandchild.
This question is similar to Force Flutter navigator to reload state when popping, but none of the solutions there work here because of the pushReplacement
(which is a hard requirement here).
Thanks!