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

Example of GoRouter usage with codegen replacement for ChangeNotifier #3314

Closed
hajajmaor opened this issue Feb 3, 2024 · 1 comment
Closed
Assignees
Labels
documentation Improvements or additions to documentation needs triage

Comments

@hajajmaor
Copy link

Using GoRouter, using refreshListenable: ref.watch(userStateProvider), must be a Listenableobject.
Using Firebase auth, as a demo, the userStateProvider will look something like this:

final ChangeNotifierProvider<UserNotifier> userStateProvider =
    ChangeNotifierProvider<UserNotifier>(
  (ChangeNotifierProviderRef<Object?> ref) => UserNotifier(ref),
);

class UserNotifier extends ChangeNotifier {
  Ref ref;
  UserNotifier(this.ref) {
    _init();
  }

  void _init() {
    FirebaseAuth.instance.authStateChanges().listen((User? user) {
      _user = user;
      if (user != null) {
      /// perform something
      } else {
      /// perform something else
      }
      notifyListeners();
    });
  }

GoRouter

import './routes.dart';

part 'router.g.dart';

@Riverpod(keepAlive: true)
GoRouter goRouter(GoRouterRef ref) {
  return GoRouter(
    routes: $appRoutes,
    debugLogDiagnostics: true,
    refreshListenable: ref.watch(userStateProvider),
  );
}

With codegen, an info is showing:
Generated providers should only depend on other generated providers. Failing to do so may break rules such as "provider_dependencies".dart(avoid_manual_providers_as_generated_provider_dependency)

How should the 'userStateProvider' be generated to use with GoRouter's refreshListenable field?

Describe why existing examples/articles do not cover this case
There is no example of using GoRouter with the use of the refreshListenable field with the codegen provider.

Additional context
Add any other context or screenshots about the documentation request here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation needs triage
Projects
None yet
Development

No branches or pull requests

2 participants