Google News
logo
Flutter - Interview Questions
What is a Flutter navigator and how is it used?
In Flutter, a Navigator is a widget used for managing a stack of route objects and navigating between them. The stack is referred to as a navigation stack or a route stack.

A route represents a screen or page in an app. The Navigator widget maintains a stack of routes that are currently on screen. When a new route is pushed onto the stack, it becomes the current route and is displayed on the screen. When a route is popped off the stack, the previous route becomes the current route and is displayed on the screen.

The Navigator class provides several methods for managing the stack of routes, including push(), pushReplacement(), pushNamed(), pop(), popUntil(), and others.

Here's an example of how to use the Navigator widget to push a new route onto the stack :
Navigator.push(
  context,
  MaterialPageRoute(builder: (context) => MyNewRoute()),
);​
In this example, the `push()` method is used to add a new route to the stack. The `builder` argument specifies the widget to be displayed on the new route.

To pop the current route off the stack and return to the previous route, you can use the `pop()` method:
Navigator.pop(context);​

The `context` argument is required in both `push()` and `pop()` methods and is used to locate the Navigator widget in the widget tree.
Advertisement