Navigation


We've touched upon programmatic navigation in the previous two sections. However, there are many more methods to get fine-grained control of navigation within your app.

AutoRouter offers the same known push, pop and friends methods to manipulate the pages stack using the generated PageRouteInfo objects. The main four types of methods are:
  • push
  • replace
  • pop
  • navigate
Use the following navigation helpers like so
// First, get the scoped router by calling
AutoRouter.of(context)
// or using the extension
context.router

// PUSH methods
// for pushing to the top of the stack
.push(BooksRoute())
.pushNamed('/books')  
// for pushing multiple pages into the stack
.pushAll([BooksRoute(), BookDetailsRoute()])
.popAndPush(BooksRoute())
.popAndPushAll([BooksRoute(), BookDetailsRoute()]) 
// pushes a route and pops the underlying pages based on a predicate
.pushAndPopUntil(BooksRoute(), (route) {return shouldPop(route);})

// REPLACE methods
// for replacing the last page in the stack (throws an error if the stack is empty)
.replace(BooksRoute())
.replaceNamed('/books')
// for replacing multiple pages into the stack
.replaceAll([BooksRoute(), BookDetailsRoute()]) 

// POP methods
// for popping last page of the current stack (unless the stack only has one entry)
.pop()
.popAndPush(BooksRoute())
.popAndPushAll([BooksRoute(), BookDetailsRoute()])
// pops until a specific page based on a predicate
.popUntil((route) {return shouldPop(route);})
.popUntilRoot()
.popUntilRouteWithName('/books') 

// NAVIGATE methods
// if the route already exists in the stack, it will pop until the
// specified route. Otherwise, if the route doesn't exist in the stack,
// then add it to the stack
// The navigate method is especially good for web apps
.navigate(BooksRoute())
.navigateNamed('/books')  
.navigateAll([BooksRoute(), BookDetailsRoute()])
Want to have more streamlined code? Use the built-in context extension methods
context.pushRoute(BooksRoute())
context.replaceRoute(BooksRoute())
context.popRoute()
context.navigateTo(BooksRoute())