What is NavigatorObserver in Flutter? | How the NavigatorObserver class works?

NavigatorObserver in Flutter

Flutter Tutorial:

Introduction

Flutter

Why Flutter

About Flutter

Cross Platform

MVVM vs MVC vs MVP

Flutter Framework

Flutter Benefits

Flutter Comparison I

Flutter Comparison II

Flutter Comparison III

Install Flutter

Android studio vs VsCode

Android Setup

VsCode Setup

Vs Code Plugins

Android Studio Plugins

Flutter Widgets:

Flutter Basic Templates

Flutter Commands

Common Widgets

Top 10 popular widgets

Flutter Stateless vs Stateful

Type of Widgets

Flutter Text

Flutter Text Style

Textfield vs TextFormField

Flutter Scaffold

Flutter Container & SizedBox

Flutter Row & Column

Flutter Buttons

Flutter Stack

Flutter Forms

Flutter AlertDialog

Flutter Icons

Flutter Images

Flutter Drawer

Flutter ListView

Flutter GridView

Flutter Toast

Flutter Checkbox

Flutter Radio Button

Flutter Progress Bar

Flutter Tooltip

Flutter Slider

Flutter Table

Flutter SnackBar

Shimmer in Flutter

Bottom Navigation Bar

Flutter Gesture

Flutter Error Handling

Flutter DropDown

Flutter Toggle

Flutter Auto Close Keyboard

Flutter Screen Size

Flutter Advance

Custom Widget in Flutter

Flutter Navigator

Flutter Read Json

Flutter Generate Excel

Flutter Multiple Widgets

Flutter Bottom sheet

Flutter Copy to Clipboard

Flutter Tab bar

Flutter Code Editor

Flutter youtube Player

Flutter REST API

Flutter http

Flutter dio

dio vs http

Advanced Concepts

Tips Flutter App Development

Flutter App version Update

Flutter Copy Text in App

Flutter Handle Null Value

Flutter Splash Screen

Flutter Disposable

Notification Listener

Flutter Switch Cases

Flutter Slivers

Flutter Custom Appbar

Databinding in Flutter

Flutter Cards

Wrap vs Builder vs OverBarFlow

Flutter App Upgrade

GoogleMap vs FlutterMap

Circular progress contain Icon

DropDown Timer in Flutter

Flutter State management Comparison

Flutter vs Other Framework

Flutter Mixin

Flutter Database

Flutter Database

Suitable DB for Flutter

DBs for Flutter

Backend for flutter

SharedPreferences

Flutter Token Expired Handling

Flutter Provider

Flutter Provider Tutorial

Flutter GetX

Flutter GetX tutorial

Flutter with Native

Flutter FFI

Flutter Testing

Pass values in Flutter

WorkManager

Flutter Tips:

Best Practices

Reduce Flutter Screens

Tips to make app smart

Optimize App

Handle Multiple Pages

Interview Questions

Top 10 Interview Questions

Dart Interview Questions

Flutter 100 Interview Questions

Flutter 20 Interview Questions

Provider Interview Questions

GetX interview Questions

BLoC interview Questions

Navigator Observer

NavigatorObserver is a one the Flutter framework class that allows to observe changes in the navigation stack. It works for observing events related to navigation stack.

In simple language, NavigatorObserver is a class that listens to navigation events such as when a new route is pushed, popped, or replaced onto the navigation stack. It is used to perform various tasks like analytics tracking, logging, or even updating UI components when a navigation event occurs.

The NavigatorObserver is abstract class in Flutter Framework and defines a set of methods.

didPush(route, previousRoute): This method is called when a new route is pushed onto the navigation stack.
didPop(route, previousRoute): This method is called when a route is popped off the navigation stack.
didRemove(route, previousRoute): This method is called when a route is removed from the navigation stack.
didReplace(newRoute, oldRoute): This method is called when a route is replaced by another route.

To use NavigatorObserver in your app, we need to create a class that extends NavigatorObserver and implement the necessary functions.

import 'package:flutter/material.dart';

class MyObserver extends NavigatorObserver {

//This method push to new screen/page
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPush(route, previousRoute);
    print('New route pushed: ${route.settings.name}');
  }

//This method move back to previous page
  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    super.didPop(route, previousRoute);
    print('Route popped: ${route.settings.name}');
  }
}

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(title: Text('My App')),
      body: Center(
        child: Text('Hello World!'),
      ),
    ),
    navigatorObservers: [MyObserver()], // Here we use navigatorObservers
  ));
}

In the above example, MyObserver that extends NavigatorObserver. We then override the didPush and didPop methods to print a message to the console whenever a new route is pushed onto the stack, or when a route is popped off the stack.

NavigatorObserver class provides a powerful way to customize the behavior of the Navigator widget in your Flutter app, and to respond to changes in the navigation stack in a flexible and customized way.

Leave a Reply

Your email address will not be published. Required fields are marked *

web_horizontal
About Us ♢ Disclaimer ♢ Privacy Policy ♢ Terms & Conditions ♢ Contact Us

Copyright © 2023 ResearchThinker.com. All rights reserved.