Skip to content

Conversation

@nate-thegrate
Copy link
Contributor

Avoid exposing API oceans

A smaller API surface is easier to understand.



This pull request allows NavigatorState methods to benefit from @protected, without needing to add @protected anywhere!

mini-design doc: flutter.dev/go/implicitly-protected

Before

navigator before this PR

After

navigator after this PR
(the result after this PR and #157873 are merged)



This is possible thanks to Dart's extension types feature.
NavigatorState (originally spanning over 2,000 lines of code) has been split into an "internal" and "external" type.

extension type NavigatorState._(_NavigatorState _state) implements State<Navigator>, TickerProvider {}

class _NavigatorState extends State<Navigator> with TickerProviderStateMixin, RestorationMixin {}

The extension type wrapper exposes State<Navigator> instead of the concrete implementation. Static analysis is now performed against the base State class, so whether the overridden methods are @protected doesn't matter!

@github-actions github-actions bot added framework flutter/packages/flutter repository. See also f: labels. f: routes Navigator, Router, and related APIs. labels Oct 30, 2024
@nate-thegrate nate-thegrate added the refactor Improving readability/efficiency without behavioral changes label Oct 30, 2024
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

This implies that we should probably do a similar thing for all public State instances in the framework, and because that would be such a big departure from the spirit of the styleguide's advice on extension, I think we should go with the explicit @protected solution #157313 instead.

@nate-thegrate
Copy link
Contributor Author

Closing this one in favor of #157313.

@nate-thegrate nate-thegrate deleted the navigator-state branch November 11, 2024 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

f: routes Navigator, Router, and related APIs. framework flutter/packages/flutter repository. See also f: labels. refactor Improving readability/efficiency without behavioral changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants