Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Navigation pop to index 1

Tags:

flutter

dart

I am recursively adding routes to the navigator. There could be 20 views or more. Pop works as advertised, but I would like to pop to index 1 and remove all push history. is there a way to replace this pop command with something like... returntoIndex0...

      new ListTile(
        title: new RaisedButton(
          child: new Text("POP"),
          onPressed: () {
            var route = new MaterialPageRoute(
              builder: (BuildContext context) =>
                  new NextPage3(value:"hi there from 3"),
            );
            Navigator.pop(context);
          },
        ),
      ),
like image 201
IrishGringo Avatar asked Apr 05 '18 12:04

IrishGringo


People also ask

How do you return a value from pop Flutter?

To return data to the first screen, use the Navigator. pop() method, which accepts an optional second argument called result . Any result is returned to the Future in the SelectionButton.

Will a pop scope Flutter?

What is the WillPopScope widget? The WillPopScope widget comes with the Flutter framework. It gives us control over the back button action, allowing the current page to go back to the previous one if it meets certain requirements. This is achieved using a callback, which the widget takes in as one of its parameters.


2 Answers

If you do not use named routes, you can use

Navigator.of(context).popUntil((route) => route.isFirst);
like image 110
Edwin Liu Avatar answered Oct 21 '22 01:10

Edwin Liu


In case you know exactly how many pops should be performed:

For example for 2 pops:

count = 0;
Navigator.popUntil(context, (route) {
    return count++ == 2;
});
like image 110
mehrdad seyrafi Avatar answered Oct 21 '22 01:10

mehrdad seyrafi