appBar back button/update List in other class - flutter

In my project I move from the current page (2) to the previous one (1) an updated a List, through the constructor, with the back button in the appBar
onPressed: () => Navigator.pushReplacement (
            context,
            new MaterialPageRoute (
                builder: (context) => new PagesListPage (
                      pagesList: list Pages,
                    )),
          )
The data is updated correctly on the previous page
but Flutter rightly 'duplicates' the previous page (1) in the stack, that is:
on page 1 - tap on button to go to page 2 ---> I see page 2
on page 2 - tap on button back in appBar (navigator.pushReplacement) ---> I see page 1
on page 1 - tap on button back in appBar ---> I see again page 1
How can I eliminate the double occurrence of page 1 in the stack?
Or alternatives to update the List on page 1 from page 2.
I'm starting out with Flutter, and I've also tried with Provider, but if I understand correctly it can only update a 'value', I couldn't get it to update a 'List'!
Thanks for any valuable suggestions

On page 1 navigator:
buttonPressPage1() async {
var updatedData = await Navigator.pushReplacement(
context, new MaterialPageRoute(builder: (context) => page1));
setState(() {
oldData = updatedData;
});
}
On page 2 navigator:
buttonPressPage2() {
Navigator.of(context).pop(updatedData);
}
Using await to you can update the data.

Related

flutter pushReplacement page

This is my second page button, go to page 3.
onPressed: () {
Navigator.push(context, MaterialPageRoute(builder:
(context) => MapsPage(index: widget.index))
);
},
This is the page 3 button that will send information back to page 2, but when the second page is pressed to go back to page 1, it has to be pressed twice. It's like replacing a new face now.
void saveAddress() {
var lat = centerMap.latitude;
var lng = centerMap.longitude;
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (_) => EditAddressPage(
index: widget.index,
lat: lat.toString(),
lng: lng.toString(),
),
));
print("Saving address: Latitude: $lat, Longitude: $lng");
}
The pushReplacement method replaces the current route on the navigation stack with a new route, rather than pushing a new route on top of the stack. This means that when you press the back button, you are not going back to the previous route (Page 2), but instead going back to the route that was replaced (Page 1).
To fix this issue, you can use the push method instead of pushReplacement when navigating from Page 3 to Page 2. This will add a new route to the navigation stack, rather than replacing the current route, and allow you to navigate back to Page 2 when the back button is pressed.

Navigation in in flutter

I have a request from a client who wants to skip one page in opening under some conditions and immediately open another page. However the problem is that when it goes back it wants to always show 2 pages. So page 1 opens page 3 and when I go back from page 3 it goes to page 2 and then station 1. I'm new to flutter and I don't know how to do it.
Use pushReplacement for such Navigation
// In Screen 1
Navigator.pushReplacement(context, Screen3()) // Or Screen2()
// In Screen 3
Navigator.pushReplacement(context, Screen2())
// In Screen 2
Navigator.pushReplacement(context, Screen1())
From page 1 to page 3 your can use
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Page3()),
Then if you want to go to Page 2 from page 3 use WillPopScope in page 3
return WillPopScope(
onWillPop: (){},
child: Scaffold(
body: Column(
children: [],
),
),
);
Add the above code in page 3. Add condition to check if it should go to Page 1 or 2 in onWillPop and again use navigator.pop or navigator.pushReplacement

How to call a function to update a value after popping a screen in Flutter?

Screen 1: shows list of item with add button.
Screen 2: form to add a new item to the list.
Screen 2 >> Screen 1 - While calling navigator.pop() in screen 2, how to call method/setState (to update list) in screen 1?
Can anyone help me out?
I don't want to relaunch screen again. I just need to run a method to update my list after popping previous screen?
When you navigate from Screen 1 to Screen 2, you use the push method. The push method returns a Future object.
You can use the then method of Future to execute your code after Screen 2 was popped from the navigation stack.
Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) => Screen2(),
))
.then((value) {
// you can do what you need here
// setState etc.
});
You can use Provider or BLoc or you can await for the result when you push the page
You can do it like this :
// this method waits for the pop to be called
var data = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
);
debugPrint(data);
// here the second argument is the data
Navigator.pop(context, "data"); // popped from LoginScreen().
output
data
Same method can also be done like below
// this method waits for the pop to be called
Navigator.push(
context,
MaterialPageRoute(builder: (context) => LoginScreen()),
).then((data){
// then will return value when the loginScreen's pop is called.
debugPrint(data);
});
Here is a good article to look into this
https://medium.com/flutter-community/flutter-push-pop-push-1bb718b13c31
Navigator.pop has a second argument called result.
You could then return the value that you need to the first page that will read it as the return value of Navigator.push

How to jump to specific page in PageView?

I have two Stateful Widget classes. In one of them is a PageView. I have no problem going between pages in this pageview. I have a setstate with an int which holds the position of the current page and a controller on the pageview. From my second class I have one of these
Navigator.push(context, MaterialPageRoute(builder: (context) => SwitchPages()));
This call brings me back to the first page of the pageview, but id like to go to a different page. Im sure theres a simple way but ive tried
setState(() {
ind = 1;
controller.jumpToPage(1);
});
But it gives me an error that the controller has not been assigned and there are no positions. Any sugguestions?

I want my button to navigate back to my home page not to the immediate previous page in flutter

I want my flutter app to navigate from my last screen back to the home page. Currently, it navigates back to the previous page which I do not want.
onPressed: () {
Navigator.pop(context, MaterialPageRoute(builder: (context) => MyHomePage()));
},
I expect the button to return me back to the Home Page
If you don't use named routes you can simply replace your code with this:
onPressed: () {
Navigator.of(context).popUntil((route) => route.isFirst);
},
This will pop the navigation stack until it reaches the first item which might be your home page.
Can't you just use Navigator.pushAndReplace(MaterialPageRoute(builder: (context) => MyHomePage())); for navigation back to the home page?