GregoryConrad / rearch-dart

Re-imagined approach to application design and architecture
https://pub.dev/packages/rearch
MIT License
92 stars 4 forks source link

Possible bug on `use.automaticKeepAlive` II #199

Closed busslina closed 5 months ago

busslina commented 5 months ago

I have a PostsScreen where I show an undefined number of posts on a Scrollable widget. use.automaticKeepAlive seems working on preventing not visible children to keep alive.

The error came when I leave this screen for another, I got an exception that seems to indicate that a similar issue to https://github.com/GregoryConrad/rearch-dart/discussions/194 is happening. If I comment use.automaticKeepAlive, the error dissapears.

Error stack trace:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown building
Overlay-[LabeledGlobalKey<OverlayState>#415c9](state:
OverlayState#cb7f3(entries: [OverlayEntry#f87d6(opaque: true;
maintainState: false),
OverlayEntry#db81d(opaque: false; maintainState: true)])):
setState() or markNeedsBuild() called during build.
This PostWidget widget cannot be marked as needing to build
because the framework is already in the
process of building widgets. A widget can be marked as
needing to be built during the build phase
only if one of its ancestors is currently building. This
exception is allowed because the framework
builds parent widgets before children, which means a dirty
descendant will always be built.
Otherwise, the framework might not visit this widget during
this build phase.
The widget on which setState() or markNeedsBuild() was called
was:
  PostWidget-[<'8eb9a794-0e3b-493d-8c1e-34d30ae254bf'>]
The widget which was currently being built when the offending
call was made was:
  Overlay-[LabeledGlobalKey<OverlayState>#415c9]

The relevant error-causing widget was:
  Navigator-[LabeledGlobalKey<NavigatorState>#4fd0d]
  Navigator:file:///C:/Users/bussl/AppData/Local/Pub/Cache/ho
  sted/pub.dev/beamer-1.6.1/lib/src/beamer_delegate.dart:767:
  16

When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/err
ors.dart 296:3       throw_
packages/flutter/src/widgets/framework.dart 5047:9
<fn>
packages/flutter/src/widgets/framework.dart 5058:14
markNeedsBuild
packages/flutter_rearch/src/widgets/consumer.dart 151:5
rebuild
packages/rearch/src/side_effects.dart 71:12
setter
packages/flutter_rearch/src/side_effects/keep_alive.dart 34:7
<fn>
packages/flutter_rearch/src/widgets/consumer.dart 114:7
deactivate
packages/flutter/src/widgets/framework.dart 2095:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart
212:7                 forEach]
packages/flutter/src/widgets/sliver.dart 1051:43
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6867:9
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6867:9
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6867:9
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/layout_builder.dart 86:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6867:9
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6867:9
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 6755:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 5539:7
visitChildren
packages/flutter/src/widgets/framework.dart 2097:12
_deactivateRecursively
packages/flutter/src/widgets/framework.dart 2109:7
add
packages/flutter/src/widgets/framework.dart 4382:5
deactivateChild
packages/flutter/src/widgets/framework.dart 4071:11
updateChildren
packages/flutter/src/widgets/framework.dart 6929:17
update
packages/flutter/src/widgets/framework.dart 3827:14
updateChild
packages/flutter/src/widgets/framework.dart 5512:16
performRebuild
packages/flutter/src/widgets/framework.dart 5650:11
performRebuild
packages/flutter/src/widgets/framework.dart 5203:7
rebuild
packages/flutter/src/widgets/framework.dart 2905:18
buildScope
packages/flutter/src/widgets/binding.dart 1136:9
drawFrame
packages/flutter/src/rendering/binding.dart 443:5
[_handlePersistentFrameCallback]
packages/flutter/src/scheduler/binding.dart 1392:7
[_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1313:9
handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1171:5
[_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1404:5
invoke
lib/_engine/engine/platform_dispatcher.dart 307:5
invokeOnDrawFrame
lib/_engine/engine/initialization.dart 187:36
<fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/ope
rations.dart 426:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/ope
rations.dart 431:39  dcall
GregoryConrad commented 5 months ago

Would you be able to provide a minimal reproducible example, where commenting out the keep alive causes it to work, but uncommenting causes it to fail?

Note to future self: I wonder if navigation to the new page causes the previous page to deactivate, causing some sort of issue with the keep alives there (rebuild triggered in deactivate maybe)?

busslina commented 5 months ago

I reproduced it in a simple repo test: https://github.com/busslina/rearch-keep-alive-issue

Fail mode:

flutter run -d windows

No fail mode: Just comment this and execute again.

GregoryConrad commented 5 months ago

Thanks, I’ll take a look in a few hours

GregoryConrad commented 5 months ago

Looks like the root cause is that the use.data is being updated in the deactivate listener, which then throws the markNeedsBuild assert. Not sure why this isn't being triggered for the infinite_scroll test right now, but alas.

The solution here is the rawValueWrapper that I removed before. Annoying, but I'll just hack one together for the keep alive.

GregoryConrad commented 5 months ago

You're good at finding the bugs 🐛😆

busslina commented 4 months ago

You're good at finding the bugs 🐛😆

Hehe thanks :)

busslina commented 4 months ago

I confirm https://github.com/GregoryConrad/rearch-dart/pull/200 solves the issue.