Currently the only way I have to refresh the screen is using Navigator.push().
if (response.statusCode == 200) {
print(await response.stream.bytesToString());
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => this.build(context)));
}
But this only adds another screen to the stack of screens.
How can I refresh the screen after calling my print() without having to use Navigator.push()?
Thanks!
You can set your class as Stateful Widget and use:
setState(() {
// Add, edit or delete widgets
// Set a diferent value for some variable
// etc...
});
Or you can still use the Navigator but with the pushReplacement function, for example:
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) => super.widget));
use setState after request api response data,
setState is a way to dynamically change the UI.
It rebuild all current screen.
We call it inside the State Object class of the StatefulWidget.
Related
I currently have a InkWell that builds a custom widget alert dialog when pressed using the build method below.
InkWell(
onTap: () => showDialog<String>(
context: context,
builder: (BuildContext context) => Popup(),
I also need it to set the state of some Bools and Int's when pressed which usually happens when using the setstate method.
can someone please show me how to do both build a widget and setstate when the button is pressed?
what would the code look like?
cheers
Make a function and set bools before you build the dialog. Then call the method in onTap:
buildMyDialog(){
myBool = false;
myInt = 2;
showDialog<String>(
context: context,
builder: (BuildContext context) => Popup(),
);
}
If you need your current UI to use the values, setState before showing dialog in the same method.
I have a flutter application where the variable is initialized outside of the build function. In the build I have :
onPressed: (BuildContext context) async {
await showDialog(
context: context,
builder: (context) => Dialog(
...
Navigator.pop()
);
).then((_) => setState(() {
print("Test is this prints");
));
setState(){ flag = !flag }
}
The purpose of the setState is just so that the UI is updates after I close out of the dialog window. I have a util file that is makes some changes to the UI when I close out of the dialog, and I would like to see it reflected after I close out of the dialog.
For some reason, the UI is not changing even though setState should be rebuilding the widget since something changes.
When I do the .then(setState(){}), the console prints the print statement, but the UI is still not changing.
In which code or widget, you are using the flag variable.
If flag is not used in any of the widget then your UI will not be updated.
Remove multiple setState, just use one and also check usage of flag variable in your code.
.then((_) => setState(() {
flag = !flag
print("Test is this prints and updated flag value is $flag");
));
When rebuild by setState(), flag is also reset. So always flag is false.
Make it a variable inside the State class
onPressed: (BuildContext context) async {
await showDialog(
context: context,
builder: (context) => Dialog(
...
Navigator.pop()
);
).then((_) => setState(() {
print("Test is this prints");
flag = !flag
));
}
void selectNotification(String? payload) async {
print('Selected notification');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SearchScreen(),
),
);
}
I am using this method to open a screen when the notification is tapped, I get the print message fine but then the Navigator message shows:
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
Im just trying to have it so if the notificaiton is tapped from foreground, background where ever the app then just pops open that screen but I cant get it to work
void selectNotification(String? payload, BuildContext context) async {
print('Selected notification');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => SearchScreen(),
),
);
}
Use getX package and use navigation routing:
something like this:
Get.put(()=> **your page**
getX package
Is there any way to refresh the previous page/ stack when Navigator.pop(context) is called? I need to do the API calling of the previous page to refresh the state of the page. The navigator.pop will sometimes be an alert dialog or maybe a back button. Is there a way to do the API calling? ThankYou.
use the then function after you push another route.
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => MyHomePage(),
),
).then(
(value) {
if (value) {
//refresh here
}
},
);
and when you return to previous screen, provide the pop function a value that determines whether to perform some action.
Navigator.of(context).pop(true);
Previous page
void goToNextPage()async {
var refresh = await Navigator.push(_context, new MaterialPageRoute(
builder: (BuildContext context) => new nextPage(context))
);
if(refresh ) setState((){});
}
NextPage
Navigator.pop(context, true);
In my Flutter app, I need to clear the Navigator stack and get back to the Home page when a certain button is pressed. To achieve that I've used Navigator.pushNamedAndRemoveUntil(context, "/", (r) => false);. I also need to call a function after the navigation has been completed, meaning that I'm now on the Home page.
I've tried calling the .whenComplete() method on Navigator.pushNamedAndRemoveUntil(), but it doesn't seem to work.
Thanks in advance.
I'd say use your function inside dipose(), so it's called when the widget is removed from the tree as you navigate to another screen.
class _MyPageState extends State<MyPage> {
// ...
// Some code
#override
void dispose() {
// Insert your function here
super.dispose();
}
// ...
}
Using didPush() method
This can be used to call your function after navigating to another screen because it returns when the push transition is complete. However, you have to do it with pushAndRemoveUntil() instead of pushNamedAndRemoveUntil(). So, you can create a PageRoute which provides didPush() method.
// Create your route
MaterialPageRoute route = MaterialPageRoute(
builder: (context) => HomePage(),
);
// Push the route onto the navigator, and then remove all the previous routes
Navigator.pushAndRemoveUntil(context, route, (r) => false);
// This returns when the push transition is complete.
route.didPush().whenComplete(() {
// Insert your function here
});
You can try as well as this code And please let me know.
gotoNewPage(context) async {
await Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
NewPage()));
/// Your logic is here.
}