Skip to content

Conversation

@goderbauer
Copy link
Member

No description provided.

/// State<RouteAwareWidget> createState() => new RouteAwareWidgetState();
/// }
/// ```
class RouteObserver<T extends Route<dynamic>> extends NavigatorObserver {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the "if (route is T && previousRoute is T)" part of the API here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Firebase Analytics use case we'd like to be informed about PageRoutes that are pushed onto other PageRoutes (or popped from them) to track page transitions (see https://github.com/flutter/plugins/blob/master/packages/firebase_analytics/lib/observer.dart#L51). This RouteObserver enables us to do just that.

During the initial PR we saw that a PageRouteObserver could be generalized to observe any type of route changes and what you see here is the result of that. If you want to be informed about route changes of any type, you use a RouteObserver<Route>. If you are only interested PageRoute changes, you use RouteObserver<PageRoute>.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, if we don't want this in the framework, we can also just put it in the Firebase Analytics Plugin (and then duplicate it for the Google Analytics Plugin). However, my discussion with @collinjackson indicated that it makes sense to have this logic in the framework to share it across the plugins.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't tell me, put it in the documentation. :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/// ```dart
/// class RouteAwareWidgetState extends State<RouteAwareWidget> with RouteAware {
///
/// final RouteObserver routeObserver = new RouteObserver();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why instantiate this class rather than put all its logic into RouteAware?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the documentation states, RouteObserver is a NavigatorObserver, so you'd most likely only instantiate one, register it with the Navigator and then register multiple Widgets (that want to be aware of changes to their current route) to the RouteObserver.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make that clear in the sample code then. You should assume that the sample code is what will be copied-and-pasted. It should be the most pedantically correct thing, because it represents the best that we can hope to see in deployed code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, but I think that made the example worse. Previously, the example had a clear focus: How do I implement RouteAware correctly in a widget. Now, the example is convoluted with other stuff (e.g. how to register the RouteObserver with the Navigator, something that is not specific to the RouteObserver and common to all NavigatorObservers).

@goderbauer goderbauer changed the title fix sample code Fix sample code and update docs Jul 17, 2017
/// on top of their own route of type `T` or popped from it. This is for example
/// useful to keep track of page transitions, e.i. a `RouteObserver<PageRoute>`
/// will inform subscribed [RouteAware]s whenever the user navigates away from
/// the current page route to another page route.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a weird feature. What if they're going from a PageRoute to a DialogRoute to a PageRoute to a PageRoute? Why should they only be notified once?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For analytics, you mostly care about transition from one full-screen page to another (at least for this use case). You don't care about transient routes (e.g. an error dialog informing you that your phone number is required to have 10 digits should not show up as a page transition in analytics). Those transient route-specific sub-routes should be popped off the navigation stack before moving to the next page route (and from my experience that's how almost all apps behave).

That's how leafy wants Google Analytics to be wired up and that also made the most sense to me when wiring up Firebase Analytics.

If you really want to be informed about all route changes, you're free to instantiate RouteObserver<Route> as mentioned in the docs in the paragraph below.

/// [RouteAware] in its [State] and subscribe it to the [RouteObserver]:
///
/// ```dart
/// // Register the RouteObserver as a navigation observer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extraneous space

///
/// }
///
/// class RouteAwareWidget extends StatefulWidget {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should put this before the State

/// ## Sample code
///
/// To make a [StatefulWidget] aware of its current [Route] state, implement
/// [RouteAware] in its [State] and subscribe it to the [RouteObserver]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/the/a/, I think.

@flutter flutter deleted a comment from googlebot Jul 19, 2017
@flutter flutter deleted a comment from googlebot Jul 19, 2017
@Hixie
Copy link
Contributor

Hixie commented Jul 20, 2017

LGTM

@Hixie
Copy link
Contributor

Hixie commented Jul 20, 2017

Bonus points if you also document all the other members on RouteObserver. :-)

@goderbauer
Copy link
Member Author

Bonus points if you also document all the other members on RouteObserver. :-)

Done. Where can I claim my bonus points? :)

@Hixie
Copy link
Contributor

Hixie commented Jul 20, 2017

I believe you can redeem them at the gift shop. :-)

new changes LGTM

@goderbauer goderbauer merged commit 8e38848 into flutter:master Jul 20, 2017
@goderbauer goderbauer deleted the sampleCodeFix branch July 20, 2017 21:26
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants