riverpod icon indicating copy to clipboard operation
riverpod copied to clipboard

Tried to read [Provider] from a place where one of its dependencies were overridden but the provider is not. When using 2 ProviderScope

Open GP4cK opened this issue 1 year ago • 9 comments

Hello

Describe the bug When trying to override a provider in tests, if I have 2 ProviderScope in my widget tree, I get the error message:

Tried to read AutoDisposeProvider<String>#cc8cc(hello world) from a place where one of its
dependencies were overridden but the provider is not.

To fix this error, you can add add "dependencies" to AutoDisposeProvider<String>#cc8cc(hello world)
such that we have:

final a = Provider(...);
final b = Provider((ref) => ref.watch(a), dependencies: [a]);

To Reproduce

Here's the code:

// main.dart
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final a = Provider.family.autoDispose<String, String>((ref, value) {
  return value;
});

final b = Provider.family.autoDispose<String, String>((ref, value) {
  return ref.watch(a(value));
}, dependencies: [a]);

void main() {
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends ConsumerWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return MaterialApp(
      home: Text(ref.watch(b('hello world'))),
    );
  }
}

// widget_test.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:riverpod_family_dependencies/main.dart';

void main() {
  testWidgets('Test scoping provider', (WidgetTester tester) async {
    const text = 'please help me Remi';
    await tester.pumpWidget(
      ProviderScope( // If I remove this one, then the test will pass
        child: ProviderScope(
          overrides: [a('hello world').overrideWithValue(text)],
          child: const MyApp(),
        ),
      ),
    );
    expect(find.text(text), findsOneWidget);
  });
}

I've created a sample repo with 2 branches which both have the issue.

  • main is using ^1.0.4
  • 2.1.1 is using... ^2.1.1 https://github.com/GP4cK/riverpod_family_dependencies

Expected behavior I would expect to be able to override a provider even if I have multiple ProviderScope in the widget tree as long as the ProviderScope that does the override is higher than the widget which is reading the provider.

GP4cK avatar Nov 03 '22 03:11 GP4cK