skip to Main Content

I’m trying to provide an instance of GoRouter with riverpod. However, this results in the "The riverpod_generator package does not support ChangeNotifier values" warning to be displayed:

enter image description here

My question is, what would be the correct way of achieving this?

2

Answers


  1. See this excellent article by Andrea: https://codewithandrea.com/articles/flutter-navigate-without-context-gorouter-riverpod/

    Basically, what you want is a non-generator with the GoRouter object itself:

    final goRouterProvider = Provider<GoRouter>((ref) {
      return GoRouter(...);
    });
    
    Login or Signup to reply.
  2. The warning is here to warn you about how Riverpod will neither listen to the ChangeNotifier nor dispose of it when the state is destroyed (which is what would happen if you were to use ChangeNotifierProvider).

    If you do not care about these points, you can safely ignore the lint.
    I’d recommend disposing the notifier manually as followed:

    @riverpod
    // ignore: unsupported_provider_value
    GoRouter example(ExampleRef ref) {
      final router = GoRouter(...);
      ref.onDispose(router.dispose);
      return router;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search