Skip to content
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

Cannot watch / select inside go_router redirect #892

Closed
EArminjon opened this issue Oct 26, 2024 · 4 comments
Closed

Cannot watch / select inside go_router redirect #892

EArminjon opened this issue Oct 26, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@EArminjon
Copy link

EArminjon commented Oct 26, 2024

Describe the bug

======== Exception caught by foundation library ====================================================
The following assertion was thrown while dispatching notifications for GoRouteInformationProvider:
Tried to use `context.select` outside of the `build` method of a widget.

Any usage other than inside the `build` method of a widget are not supported.
'package:provider/src/inherited_provider.dart':
Failed assertion: line 266 pos 12: 'widget is LayoutBuilder || debugDoingBuild'

go router documentation :

/// If [BuildContext.dependOnInheritedWidgetOfExactType] is used during the
/// redirection (which is how `of` method is usually implemented), a
/// re-evaluation will be triggered if the [InheritedWidget] changes.
final GoRouterRedirect? redirect;

context.watch and context.select use dependOnInheritedWidgetOfExactType but an assert prevent them to be used inside go_router redirect.

If I edit provider code as bellow it works well, redirection are well re-evaluated as expected (it's maybe not the good solution, I just did it for demo purpose) :

image

To Reproduce

Call context.watch or context.select inside GoRouter route redirect callback.

Expected behavior
Context.watch or context.select should be callable inside GoRouter redirect.

@EArminjon EArminjon added bug Something isn't working needs triage labels Oct 26, 2024
@rrousselGit
Copy link
Owner

I cannot reasonably depend on go_router just for an assert.

So this would require removing the assert entirely, which I rather would not do.

@EArminjon
Copy link
Author

EArminjon commented Oct 26, 2024

Hello @rrousselGit

Just to notice and to avoid any confusion: Router is coming from flutter/lib/src/widgets/router.dart, the 'solution' is not linked to GoRouter.

@rrousselGit
Copy link
Owner

If it's Router from Flutter, maybe.
Still, I'd like not to change this value too much.

To begin with, the issue is State.didChangeDependencies here.

This lifecycle isn't supported, regardless of Router.

@EArminjon
Copy link
Author

EArminjon commented Oct 26, 2024

I think you refer to the discussion i believe flutter/flutter#49505 ?

As an alternative we can use GoRouter refreshListenable as context watch/select will not be supported.

We can close this issue so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants