I am writing an application on Flutter. I need to do popAndPushNamed but I also need to pass arguments to the Page I am pushing. How can I do? Is there another way to pop and push or I have to look for a different solution? This thing is making me insane.
Thanks in advance!
For sending data to widget you can use it's constructor
String result = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => SecondWidget("String")));
In this case result
is new string, which will be returned from SecondWidget
class SecondWidget extends StatefulWidget {
SecondWidget(this.text);
final String text;
And somewhere in SecondWidgetState
:
Navigator.pop(context, "New string");
This will close SecondWidget
and return value for result
I think it's clear enough and it'll help
UPD
How to do in your case (something like this)
Profile profile = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginWidget()));
if (profile != null) Navigator.of(context).push(MaterialPageRoute(builder: (context) => MainWidget(profile)));
In LoginWidget
:
void _afterLogIn() {
// do something
Navigator.pop(context, this.receivedProfile);
}
In MainWidget
:
class MainWidget extends StatefulWidget {
MainWidget(this.profile);
Profile profile;