Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Navigator.pushReplacement with variable result property

I am trying to pass a value to a Widget down the Navigator stack. Sample code:

class FirstPage extends StatefulWidget{
//...

receivedValue = await Navigator.push<String>(context, CupertinoPageRoute(
  (ctx) => SecondPage(),
));

}


class SecondPage extends StatefulWidget{
//...

receivedValue = await Navigator.pushReplacement<String,String>(context, CupertinoPageRoute(
  (ctx) => ThirdPage(),
));

}



class ThirdPage extends StatefulWidget{
//...
Navigator.pop<String>(context, outputString);
}

However the value is not passed to the first screen unless I keep the SecondPage in the stack. Reading the docs, there is a result property in pushReplacement that passes back to the previous screen in the navigator stack. In case of the replacing screen is popped.

However, the value I want to pass depends on user input in ThirdScreen. Is there a way to do it?

like image 424
Mudassir Avatar asked Mar 20 '26 13:03

Mudassir


1 Answers

Instead of pushReplacement, you can just push the third page and pop the second page after the third page is popped!

class FirstPage extends StatefulWidget{
//...

receivedValue = await Navigator.push<String>(context, CupertinoPageRoute(
  (ctx) => SecondPage(),
));

}


class SecondPage extends StatefulWidget{
//...

receivedValue = await Navigator.push<String,String>(context, CupertinoPageRoute(
  (ctx) => ThirdPage(),
));

Navigator.pop(context, receivedValue);
}



class ThirdPage extends StatefulWidget{
//...
Navigator.pop<String>(context, outputString);
}
like image 92
Mahdi Arabpour Avatar answered Mar 23 '26 03:03

Mahdi Arabpour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!