River_pod: ScopedProvider accessed after being disposed

Created on 23 Sep 2020  Â·  5Comments  Â·  Source: rrousselGit/river_pod

Describe the bug
RiverPod tries to access a ScopedProvider after it has been disposed.

To Reproduce

My app is pretty complex and involved.. the only notable thing I'm doing is having a ScopedProvider that is not overridden, but just returns the result of some computations based on other ScopedProviders.

Expected behavior
Should not throw Exception

Notes

From the call stack, it would appear that RiverPod is indeed trying to read after a dispose:

ProviderContainer.readProviderElement (OMITTED\Pub\Cache\hosted\pub.dartlang.org\riverpod-0.11.0\lib\src\framework\container.dart:279)
ProviderContainer._disposeProvider (OMITTED\Pub\Cache\hosted\pub.dartlang.org\riverpod-0.11.0\lib\src\framework\container.dart:184)
_AutoDisposer._performDispose (OMITTED\Pub\Cache\hosted\pub.dartlang.org\riverpod-0.11.0\lib\src\framework\auto_dispose.dart:134)
_AutoDisposer.scheduleDispose.<anonymous closure> (OMITTED\Pub\Cache\hosted\pub.dartlang.org\riverpod-0.11.0\lib\src\framework\auto_dispose.dart:113)
new Future.microtask.<anonymous closure> (dart:async/future.dart:201)
_rootRun (dart:async/zone.dart:1182)
_CustomZone.run (dart:async/zone.dart:1093)
_CustomZone.runGuarded (dart:async/zone.dart:997)
_CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037)
_rootRun (dart:async/zone.dart:1190)
_CustomZone.run (dart:async/zone.dart:1093)
_CustomZone.runGuarded (dart:async/zone.dart:997)
_CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037)
_microtaskLoop (dart:async/schedule_microtask.dart:41)
_startMicrotaskLoop (dart:async/schedule_microtask.dart:50)

StackTrace

E/flutter ( 6796): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Bad state: Tried to read a provider from a ProviderContainer that was already disposed
E/flutter ( 6796): #0      ProviderContainer.readProviderElement 
package:riverpod/…/framework/container.dart:279
E/flutter ( 6796): #1      ProviderContainer._disposeProvider 
package:riverpod/…/framework/container.dart:184
E/flutter ( 6796): #2      _AutoDisposer._performDispose 
package:riverpod/…/framework/auto_dispose.dart:134
E/flutter ( 6796): #3      _AutoDisposer.scheduleDispose.<anonymous closure> 
package:riverpod/…/framework/auto_dispose.dart:113
E/flutter ( 6796): #4      new Future.microtask.<anonymous closure>  (dart:async/future.dart:201:37)
E/flutter ( 6796): #5      _rootRun  (dart:async/zone.dart:1182:47)
E/flutter ( 6796): #6      _CustomZone.run  (dart:async/zone.dart:1093:19)
E/flutter ( 6796): #7      _CustomZone.runGuarded  (dart:async/zone.dart:997:7)
E/flutter ( 6796): #8      _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:1037:23)
E/flutter ( 6796): #9      _rootRun  (dart:async/zone.dart:1190:13)
E/flutter ( 6796): #10     _CustomZone.run  (dart:async/zone.dart:1093:19)
E/flutter ( 6796): #11     _CustomZone.runGuarded  (dart:async/zone.dart:997:7)
E/flutter ( 6796): #12     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:1037:23)
E/flutter ( 6796): #13     _microtaskLoop  (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 6796): #14     _startMicrotaskLoop  (dart:async/schedule_microtask.dart:50:5)
bug

Most helpful comment

Wow that was a hard one

That minimal repro was super helpful, thanks a lot for that.

All 5 comments

I need a way to reproduce the error, otherwise I can't do anything

Yeah sorry, didn't have time yesterday. You can check this minimal repro I just published.

Bumpity bump.. I'd really like for this to be fixed, since using StateProvider as an alternative requires quite the rework.

Slightly off-topic but related: it would be nice to have a mechanism to update a provider's state similar to ScopedProvider's override. That way we could avoid the "rebuild triggered while widget was building" error and not depend on ScopedProvider. Or maybe this is wrong? Just a suggestion anyway 😄

Wow that was a hard one

That minimal repro was super helpful, thanks a lot for that.

Thanks to you for the fix! 🎉 Of course feel free to close the issue whenever 😄

Was this page helpful?
0 / 5 - 0 ratings