Flutter Web and Navigation History
Flutter Web and Navigation History
How to know the navigation history in Flutter Web? Hmm…
All in one Flutter resource: https://flatteredwithflutter.com/flutter-web-and-navigation-history/
Summary
The provided content discusses managing navigation history in Flutter Web applications using a stack data structure implemented with Dart's ListQueue and integrating custom NavigatorObservers.
Abstract
The article "Flutter Web and Navigation History" delves into the technical aspects of handling navigation history in Flutter Web applications. It emphasizes the need for a stack-like data structure to keep track of navigation routes, with the ListQueue from Dart's dart:collection library being identified as a suitable choice. The author outlines the creation of an abstract class _NavStack to define stack operations such as push, pop, top, fetchAll, and clear, which are then implemented in a concrete class NavStack. The implementation ensures that navigation actions such as adding, removing, and retrieving routes are efficient and maintain the correct order. Furthermore, the article explains how to integrate this stack with Flutter's RouteObserver by extending it and implementing didPush and didPop methods to synchronize route changes with the custom stack. This integration is achieved by adding the custom navigator observer to the navigatorObservers property of the MaterialApp widget. The article also provides visual examples and links to additional resources on Flutter Web development.
Opinions
ListQueue for implementing the stack is presented as an efficient solution, highlighting the author's preference for performance and the cyclic buffer optimization provided by this data structure.RouteObserver as a means to seamlessly integrate custom navigation history tracking, showcasing the framework's extensibility and the author's proficiency in leveraging it.Flutter Web and Navigation History
How to know the navigation history in Flutter Web? Hmm…
All in one Flutter resource: https://flatteredwithflutter.com/flutter-web-and-navigation-history/
View the demo here
Website: https://web.flatteredwithflutter.com/#/
We will cover briefly about

It's quite clear, we need a data structure for storing routes.
Thought process: When we navigate between different links in a browser, the last link is usually the first to be traced back (on click of the back button).

Going 1 link to several
(Note: This assumes you are in 1 single tab only)
On backtracking

Going back now
(Note: This assumes you are in 1 single tab only)
The data structure right for use would be Stack since we need to push the routes onto the stack and pop (when we go back to the previous link).

In doing my research I found
dart:collectionAs per the documentation:
Classes and utilities that supplement the collection support in dart:core.
But did not find any explicit collection named Stack, until I stumbled upon ListQueue
List based Queue.
Keeps a cyclic buffer of elements, and grows to a larger buffer when it fills up. This guarantees constant time peek and remove operations, and amortized constant time add operations.
The structure is efficient for any queue or stack usage.
We will use ListQueue for storing our routes.
We make an abstract class first, to define the operations which we will implement for our generic ListQueue.
abstract class _NavStack<T> {
void push(T val) {} void pop() {} T top(); List<T> fetchAll(); void get clear {}
}push: For pushing a route of type T
pop: For popping the route (topmost)
top: Get the route which is on top
fetchAll: Get all the routes saved.
clear: Clear the saved routes
Let’s define our class(NavStack) which implements the above abstract class(_NavStack).
class NavStack<T> implements _NavStack<T> {}Initialize the list queue by
final ListQueue<T> _internal = ListQueue();Pushing items in this list by using the addLast function of the list queue.
_internal.addLast('SOME VALUE');addLast: Adds the items at the end. Since each new route will be the latest, we add it to the end of the list queue.
Pop the item in this list by using removeLast function of the list queue.
_internal.removeLast();removeLast: Removes and returns the last element of the queue.
Getting the topmost or latest item in this list queue by the last function
_internal.last;last: Returns the last (or topmost) element. Meaning our latest route is always at the top of the stack.
We will use the clear function of the list queue for clearing our stored routes.
For fetching items, we will loop through the length of the list, add items to a new list and return.
List<T> fetchAll() {
final _list = <T>[];
for (var i = 0; i < length; i++) {
_list.add(_internal.elementAt(i));
} return _list;
}
We have the list queue implemented from the above step.
Next, we need to place this list queue in our flutter web, in such a way that
There is a property inside MaterialApp called navigatorObservers. This implements RouteObserver internally.
RouteObserver informs subscribers whenever a route of type R is pushed on top of their own route of type R or popped from it.
RouteObserver<PageRoute> will inform subscribed RouteAwaresMethods inside RouteObserver class
didPop (whenever a route is popped)didPush (whenever a route is pushed)We will create our own navigator observers which
didPop and didPush methods
didPushwe extract the current screen name and push it into our NavStack class. This way whenever a route is pushed, it also gets added to our class.Finally, we add this custom navigator observer to MaterialApp’s property.
return MaterialApp(
// OTHER PROPERTIES navigatorObservers: [CustomRouteObserver()],
);
Interesting Articles related to Flutter Web here:
Hosted URL: https://web.flatteredwithflutter.com/#/
Tari IbabaWow I never thought the day I stop using VS Code would come so soon…
Jessica StillmanJeff Bezos’s morning routine has long included the one-hour rule. New neuroscience says yours probably should too.
Isaac ThamA practical and effective guide for evaluating AI summaries
HarendraGet a server with 24 GB RAM + 4 CPU + 200 GB Storage + Always Free