1

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:

  1. Press button on First.
  2. Press button on Second.
  3. You see "Back to First".

What I want to happen:

  1. Press button on First.
  2. Press button on Second.
  3. Press button on Third.
  4. 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!

Daniel Porteous
  • 5,536
  • 3
  • 25
  • 44

0 Answers0