-
Notifications
You must be signed in to change notification settings - Fork 27.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NavigatorObserver and Router API #124339
Comments
@goderbauer @Hixie I am curious if you have any thought on this. cc @Milad-Akarie, for auto-router |
I am not a big fan of the current NavigatorObserver API, so if we can replace it with something better, I am all for it! For any proposal it would be interesting to study how the existing use cases (mainly Heroes and analytics) can be implemented with it. |
cc @johnpryan |
I agree with the idea of implementing a new observing system (e.g.
but the whole situation indeed is much complicated than it might seem at first. There are several things to consider furthermore Multiple RoutersIn this case, the Developer ExperienceDevelopers not using Router API should be able to just keep using From this perspective, it seems that
This way the Now all developers that are currently using some Now that I've written this composite approach, I don't see any reason to prevent us from just having something like RouterObserver with CurrentConfigurationObserver implements NavigatorObserver { ... } with which the migration would be even easier - just some renaming and moving things around. (I will leave the initial composite idea so we have a full train of thought)
|
A new or an enhanced navigator observer is definitely needed for the Router Api. Considerations I have in mind
The initial implementation idea // something like _NavigatorObservation
abstract class NavigationEvent{}
// navigator push event
class NavigatorDidPush extends NavigationEvent{
final Route<dynamic> route;
final Route<dynamic> previousRoute;
NavigatorDidPush(this.route, this.previousRoute);
}
abstract class NavigationEventsObserver{
void onNavigationEvent(NavigationEvent event);
}
// possible back-compatibility with current NavigationObserver
// also mapping events to method-calls for a simpler api for newer-implementations
class NavigationObserver extends NavigationEventsObserver{
@override
void onNavigationEvent(NavigationEvent event) {
if(event is NavigatorDidPush){
didPush(event.route, event.previousRoute);
}
// ... handle other navigator 1.0 events
}
void didPush(Route route, Route? previousRoute){}
}
|
We could also consider using Notification It is currently only used for ScrollNotification, we can add a new subclass for NavigationNotification. The possible down side of it is that this do a linear walk on the ancestor widgets to reach the listener which may potentially expansive. I am not too worry about HeroController, I think we will decouple it from navigator anyway. |
The Problem
The NavigatorObserver was original built for Navigator API. After the Router API is introduced, we started to face several issues
It is common to have nested navigators( or even no navigator) with Router API. A single navigation in Route API may trigger 0~n navigators to push/pop routes. Even if we somehow listen to all navigators, it may still be impossible to piece the notifications together.
Router can achieve things like complete replace route stack in one navigation, there isn't a corresponding API in NavigatorObserver to record the event. It may produce a bunch of didPush/didPop/didReplace that are hard to make sense.
One extreme cases is something like CupertinoTabScaffold. The navigations in between tabs are achieved by using IndexedStack to move different screens around. In this cases, none of the NavigatorObserver will fire. We encounter this issue when we implements flutter/packages#2650
Proposal
A new observing system that based Router API. I don't have much detail yet, one initial idea is to have an observer that observe the Configuration
We can set the generic Type to Diagnotics. so that other third_party router package would need to make sure their Configuration implements the subtype in order to use the observer.
Things to consider
The text was updated successfully, but these errors were encountered: