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

ref.invalidate(familyProvider) is not working with scoped providers #3567

Open
thiagokisaki opened this issue May 21, 2024 · 1 comment
Open
Assignees
Labels
bug Something isn't working needs triage

Comments

@thiagokisaki
Copy link

thiagokisaki commented May 21, 2024

Describe the bug
ref.invalidate(familyProvider) doesn't seem to work properly when using scoped providers. The problem happens when nesting a ProviderScope inside others.

The reason I'm using many ProviderScopes is that I want to override providers dynamically based on a runtime check. For example, switch from a network and a local implementation of a service, in an app that works both offline with a local database or consuming an API.

I am not sure if this is a bug or a misunderstanding from my part. If I am wrong, I'd appreciate feedback on what I could do to fix the problem.

To Reproduce
I created a simple todo app to demonstrate the problem. Please read the comments in the code.
https://dartpad.dev/?id=b0cbe8e4d8869e181df00ebd5873c0cc

Expected behavior
I expect ref.invalidate(familyProvider) to work even when using scoped providers.

@johnstef99
Copy link

I came across this one too, here is a simpler way to reproduce the bug only using riverpod in dart:

import 'package:riverpod/riverpod.dart';
import 'package:test/test.dart';

void main() {
  test('working invalidating family', () {
    final someProvider = Provider((ref) {
      print('someProvider');
      ref.onDispose(() => print('someProvider disposed'));
    });
    final familyProvider = Provider.family((ref, a) {
      ref.watch(someProvider);
      print('familyProvider $a');
      ref.onDispose(() => print('familyProvider $a disposed'));
    }, dependencies: [someProvider]);

    final root = ProviderContainer();
    final container = ProviderContainer(
      parent: root,
    );

    container.read(familyProvider(1));
    container.invalidate(familyProvider);
  });

  test('not working invalidating family', () {
    final someProvider = Provider((ref) {
      print('someProvider');
      ref.onDispose(() => print('someProvider disposed'));
    });
    final familyProvider = Provider.family((ref, a) {
      ref.watch(someProvider);
      print('familyProvider $a');
      ref.onDispose(() => print('familyProvider $a disposed'));
    }, dependencies: [someProvider]);

    final root = ProviderContainer();
    final container = ProviderContainer(
      parent: root,
      overrides: [
        someProvider.overrideWith((ref) {
          print('someProvider override');
          ref.onDispose(() => print('someProvider override disposed'));
        })
      ],
    );

    container.read(familyProvider(1));
    container.invalidate(familyProvider);
  });
}

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

No branches or pull requests

3 participants