Skip to content

Conversation

@chunhtai
Copy link
Contributor

@chunhtai chunhtai commented Aug 8, 2022

fixes flutter/flutter#105808
fixes flutter/flutter#99121

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot changed the base branch from go_router_v5 to main August 8, 2022 22:46
@flutter-dashboard
Copy link

This pull request was opened against a branch other than main. Since Flutter pull requests should not normally be opened against branches other than main, I have changed the base to main. If this was intended, you may modify the base back to go_router_v5. See the Release Process for information about how other branches get updated.

Reviewers: Use caution before merging pull requests to branches other than main, unless this is an intentional hotfix/cherrypick.

@chunhtai chunhtai changed the base branch from main to go_router_v5 August 8, 2022 22:48
@chunhtai
Copy link
Contributor Author

The ci is failing due to flutter sdk version mismatched, may need some more time to figure out, otherwise the code is ready

Copy link
Member

Choose a reason for hiding this comment

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

Can you use await here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sychronous future can't be used in function that marked async, that is why I refactor the entire redirect pipeline.

@chunhtai chunhtai requested a review from loic-sharma August 23, 2022 16:57
Copy link
Member

@loic-sharma loic-sharma Aug 23, 2022

Choose a reason for hiding this comment

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

Please add a comment here explaining why SynchronousFuture is used instead of Future.value.

Comment on lines 113 to 119
Copy link
Member

Choose a reason for hiding this comment

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

I'd move that redirect comment up. Also, I'd capitalize all comments and add punctuation to all comments in this file for consistency.

Suggested change
// log a user in, letting all the listeners know
StreamAuthScope.of(context).signIn('test-user');
setState(() {
loggingIn = true;
});
// router will automatically redirect from /login to / using
// refreshListenable
// Log a user in and notify all listeners.
// This will automatically redirect from /login to / using
// refreshListenable.
StreamAuthScope.of(context).signIn('test-user');
setState(() {
loggingIn = true;
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops it was a outdated comment i forgot to delete, it no longer apply

@chunhtai chunhtai requested a review from loic-sharma August 26, 2022 18:32
@chunhtai chunhtai marked this pull request as ready for review August 26, 2022 18:38
Copy link
Member

Choose a reason for hiding this comment

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

What do you think of making all these FutureOr? Benefits:

  1. Makes this breaking change a bit easier for folks migrating to v5
  2. Folks don't need to do anything special for the common synchronous scenario

Drawback: our code may be a bit more complicated.

This seems like a good trade-off given folks are unlikely to use SynchronousFuture when they should.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is also against flutter style guide https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#avoid-using-futureort

developer can also mark a synchronous method a async if they don't want to bother with SynchronousFuture, and it is not too bad, it will be introduce a one frame delay before page transition. I don't think this is worth it to breaking flutter style guide in this case.

@chunhtai
Copy link
Contributor Author

@loic-sharma PTAL :)

Comment on lines +124 to +137
You can use redirection to prevent the user from visiting a specific page. In
go_router, redirection can be asynchronous.

```dart
GoRouter(
...
redirect: (context, state) async {
if (await LoginService.of(context).isLoggedIn) {
return state.location;
}
return '/login';
},
);
```
Copy link
Member

Choose a reason for hiding this comment

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

Async redirects is a bit more of an advanced scenario. I'd personally prefer to put the simpler sync scenario here and keep async to an example.


If the code depends on [BuildContext](https://api.flutter.dev/flutter/widgets/BuildContext-class.html)
through the [dependOnInheritedWidgetOfExactType](https://api.flutter.dev/flutter/widgets/BuildContext/dependOnInheritedWidgetOfExactType.html)
(which is how `of` method is usually implemented), the redirect will be called every time the [InheritedWidget](https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(which is how `of` method is usually implemented), the redirect will be called every time the [InheritedWidget](https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html)
(which is how `of` methods are usually implemented), the redirect will be called every time the [InheritedWidget](https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html)

(which is how `of` method is usually implemented), the redirect will be called every time the [InheritedWidget](https://api.flutter.dev/flutter/widgets/InheritedWidget-class.html)
updated.

### Top-level redirect
Copy link
Member

Choose a reason for hiding this comment

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

I don't think a newcomer would understand top-level vs route-level redirects by this point. Should we update the code sample above to contain both a top-level and route-level redirect?

Widget build(BuildContext context) => MaterialApp.router(
routeInformationProvider: _router.routeInformationProvider,
routeInformationParser: _router.routeInformationParser,
routerDelegate: _router.routerDelegate,
Copy link
Member

Choose a reason for hiding this comment

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

Should we update this to use @johnpryan's RouterConfig changes once that's merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup

bool routerNeglect,
) {
List<Page<dynamic>>? pages;
if (matches.isEmpty) {
Copy link
Member

Choose a reason for hiding this comment

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

Consider adding a comment as to when this scenario happens. My guess is this includes async redirect, which isn't obvious.

await tester.pumpWidget(MaterialApp.router(
routeInformationProvider: router.routeInformationProvider,
routeInformationParser: router.routeInformationParser,
routerDelegate: router.routerDelegate,
Copy link
Member

Choose a reason for hiding this comment

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

This guy can also be updated to use RouterConfig when that's ready

Copy link
Member

@loic-sharma loic-sharma left a comment

Choose a reason for hiding this comment

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

LGTM! Great work! 🚀

Please also get a review from @johnpryan as my go router knowledge has huge gaps 😅

Copy link
Contributor

@johnpryan johnpryan left a comment

Choose a reason for hiding this comment

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

LGTM

- Fixes a bug where intermediate route redirect methods are not called.
- **BREAKING CHANGE**
- Redesigns redirection API.
- Redesigns redirection API, adds asynchronous feature, and builds context to redirect.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
- Redesigns redirection API, adds asynchronous feature, and builds context to redirect.
- Redesigns redirection API, adds asynchronous feature, and adds build context to redirect.

@chunhtai chunhtai merged commit 87dbb73 into flutter:go_router_v5 Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] Support asynchronous redirects [go_router] Add BuildContext to redirect

3 participants