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

Error Tried to read provider from a place where one of its dependencies were overridden but the provider is not. even with specified dependencies #2429

Closed
ValentinVignal opened this issue Apr 6, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@ValentinVignal
Copy link
Contributor

ValentinVignal commented Apr 6, 2023

Describe the bug

I have 3 providers:

final a = Provider.autoDispose<String>(
  (ref) {
    return 'a0';
  },
  name: 'a',
);

final b = Provider.autoDispose<String>(
  (ref) {
    return 'b0';
  },
  name: 'b',
);

final c = Provider.autoDispose<String>(
  (ref) {
    return ref.watch(a) + ref.watch(b);
  },
  name: 'c',
  dependencies: [a, b],
);

When overriding a.overrideWithValue 2 times and with a ProviderScope that gets disposed / re-initiated, it raises the error

[UncontrolledProviderScope], state: _ConsumerState#652b3):
Tried to read c0:AutoDisposeProvider<String>#a27a4 from a place where one of its dependencies were
overridden but the provider is not.

To fix this error, you can add "dependencies" to c0:AutoDisposeProvider<String>#a27a4 such that we
have:

\```
final a = Provider(...);
final b = Provider((ref) => ref.watch(a), dependencies: [a]);
\```
'package:riverpod/src/framework/container.dart':
Failed assertion: line 443 pos 17: 'targetElement.provider != targetElement.origin ||
                    dependencyElement ==
                        targetElement.container
                            .readProviderElement<Object?>(dependency)'

To Reproduce

Run the code snippet (or checkout https://github.com/ValentinVignal/flutter_app_stable/tree/riverpod/override-providers)

Code snippet
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final a = Provider.autoDispose<String>(
  (ref) {
    return 'a0';
  },
  name: 'a',
);

final b = Provider.autoDispose<String>(
  (ref) {
    return 'b0';
  },
  name: 'b',
);

final c = Provider.autoDispose<String>(
  (ref) {
    return ref.watch(a) + ref.watch(b);
  },
  name: 'c',
  dependencies: [a, b],
);

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: ProviderScope(
        overrides: [
          a.overrideWithValue('a1'), // private shell
        ],
        child: ProviderScope(
          overrides: [
            a.overrideWithValue('a2'), // Form screen
          ],
          child: const MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends ConsumerStatefulWidget {
  const MyWidget({super.key});

  @override
  ConsumerState<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends ConsumerState<MyWidget> {
  var _show = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(ref.watch(a)),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  _show = !_show;
                });
              },
              child: const Text('Toggle'),
            ),
            if (_show) ...[
              ProviderScope(
                child: Consumer(
                  builder: (context, ref, child) {
                    return Text(ref.watch(c));
                  },
                ),
              ),
            ]
          ],
        ),
      ),
    );
  }
}

Click on the button "Toggle" 3 times to

  1. Show
  2. Hide
  3. Show

The sub widget.

The 2nd time the widget is shown, it throws the error:

Logs
Launching lib/main.dart on macOS in debug mode...
2023-04-06 10:02:41.402 xcodebuild[6434:50003] DVTCoreDeviceEnabledState: DVTCoreDeviceEnabledState_Disabled set via user default (DVTEnableCoreDevice=disabled)
Building macOS application...                                           
Syncing files to device macOS...                                   122ms

Flutter run key commands.
r Hot reload. 🔥🔥🔥
R Hot restart.
h List all available interactive commands.
d Detach (terminate "flutter run" but leave application running).
c Clear the screen
q Quit (terminate the application on the device).

💪 Running with sound null safety 💪

An Observatory debugger and profiler on macOS is available at: http://127.0.0.1:51728/wRdZljTioHE=/
The Flutter DevTools debugger and profiler on macOS is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:51728/wRdZljTioHE=/

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Consumer(dirty, dependencies:
[UncontrolledProviderScope], state: _ConsumerState#1239a):
Tried to read c0:AutoDisposeProvider<String>#e63ee from a place where one of its dependencies were
overridden but the provider is not.

To fix this error, you can add "dependencies" to c0:AutoDisposeProvider<String>#e63ee such that we
have:

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

'package:riverpod/src/framework/container.dart':
Failed assertion: line 443 pos 17: 'targetElement.provider != targetElement.origin ||
                    dependencyElement ==
                        targetElement.container
                            .readProviderElement<Object?>(dependency)'

The relevant error-causing widget was:
  Consumer Consumer:file:///Users/valentin/Perso/Projects/flutter_app_stable/lib/main.dart:99:24

When the exception was thrown, this was the stack:
#2      ProviderContainer.readProviderElement.<anonymous closure> (package:riverpod/src/framework/container.dart:443:17)
#3      ProviderContainer.readProviderElement (package:riverpod/src/framework/container.dart:466:8)
#4      ProviderBase.addListener (package:riverpod/src/framework/provider_base.dart:77:26)
#5      ProviderContainer.listen (package:riverpod/src/framework/container.dart:274:21)
#6      ConsumerStatefulElement.watch.<anonymous closure> (package:flutter_riverpod/src/consumer.dart:558:25)
#7      _LinkedHashMapMixin.putIfAbsent (dart:collection-patch/compact_hash.dart:535:23)
#8      ConsumerStatefulElement.watch (package:flutter_riverpod/src/consumer.dart:551:26)
#9      _MyWidgetState.build.<anonymous closure> (package:flutter_app_stable/main.dart:101:37)
#10     Consumer.build (package:flutter_riverpod/src/consumer.dart:375:20)
#11     _ConsumerState.build (package:flutter_riverpod/src/consumer.dart:477:19)
#12     StatefulElement.build (package:flutter/src/widgets/framework.dart:5080:27)
#13     ConsumerStatefulElement.build (package:flutter_riverpod/src/consumer.dart:540:20)
#14     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4968:15)
#15     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#16     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#17     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5)
#18     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5124:11)
#19     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5)
...     Normal element mounting (7 frames)
#26     _UncontrolledProviderScopeElement.mount (package:flutter_riverpod/src/framework.dart:309:11)
...     Normal element mounting (9 frames)
#35     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
#36     MultiChildRenderObjectElement.inflateWidget (package:flutter/src/widgets/framework.dart:6512:36)
#37     Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18)
#38     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:6041:32)
#39     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6537:17)
#40     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#41     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6384:14)
#42     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#43     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#44     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#45     StatelessElement.update (package:flutter/src/widgets/framework.dart:5044:5)
#46     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#47     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#48     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#49     StatelessElement.update (package:flutter/src/widgets/framework.dart:5044:5)
#50     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#51     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#52     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#53     ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#54     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#55     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#56     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#57     ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#58     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#59     RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5981:32)
#60     MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6537:17)
#61     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#62     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#63     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#64     ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#65     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#66     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#67     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#68     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#69     StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#70     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#71     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#72     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#73     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#74     StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#75     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#76     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#77     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#78     ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#79     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#80     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#81     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#82     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#83     StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#84     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#85     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6384:14)
#86     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#87     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#88     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#89     ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#90     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#91     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6384:14)
#92     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#93     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#94     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#95     Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#96     StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#97     Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#98     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#99     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#100    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#101    StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#102    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#103    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#104    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#105    ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#106    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#107    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#108    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#109    ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#110    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#111    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#112    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#113    ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#114    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#115    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#116    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#117    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#118    StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#119    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#120    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#121    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#122    ProxyElement.update (package:flutter/src/widgets/framework.dart:5301:5)
#123    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#124    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#125    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#126    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#127    StatefulElement.update (package:flutter/src/widgets/framework.dart:5156:5)
#128    Element.updateChild (package:flutter/src/widgets/framework.dart:3660:15)
#129    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4993:16)
#130    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5133:11)
#131    Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
#132    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2743:19)
#133    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:863:21)
#134    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:381:5)
#135    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1289:15)
#136    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1218:9)
#137    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1076:5)
#138    _invoke (dart:ui/hooks.dart:145:13)
#139    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#140    _drawFrame (dart:ui/hooks.dart:112:31)
(elided 2 frames from class _AssertionError)

════════════════════════════════════════════════════════════════════════════════════════════════════

Another exception was thrown: A RenderFlex overflowed by 99741 pixels on the bottom.
Screen.Recording.2023-04-06.at.10.03.23.AM.mov

Expected behavior

I would expect it to not throw any error/fail the assert as the dependencies of c are specified.

@ValentinVignal ValentinVignal added bug Something isn't working needs triage labels Apr 6, 2023
@ValentinVignal
Copy link
Contributor Author

It looks like the version 2.3.4 of riverpod and flutter_riverpod fixed it. I'm closing the issue. Thank you for the fix!

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