google / flutter.widgets

https://pub.dev/packages/flutter_widgets
BSD 3-Clause "New" or "Revised" License
1.37k stars 478 forks source link

PageStorage.of(context) can return null in StreamBuilder #110

Open cornerman opened 4 years ago

cornerman commented 4 years ago

I have a ScrollablePositionedList in a StreamBuilder. When the stream triggers, I get an error:

I/flutter (20336): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (20336): The following NoSuchMethodError was thrown building StreamBuilder<Widget>(state:
I/flutter (20336): _StreamBuilderBaseState<Widget, AsyncSnapshot<Widget>>#5db9a):
I/flutter (20336): The method 'readState' was called on null.
I/flutter (20336): Receiver: null
I/flutter (20336): Tried calling: readState(Instance of 'StatefulElement')
I/flutter (20336): 
I/flutter (20336): The relevant error-causing widget was:
I/flutter (20336):   StreamBuilder<Widget> file:///home/cornerman/projects/listo/lib/util/stream_ext.dart:8:46
I/flutter (20336): 
I/flutter (20336): When the exception was thrown, this was the stack:
I/flutter (20336): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
I/flutter (20336): #1      _ScrollablePositionedListState.initState (package:scrollable_positioned_list/src/scrollable_positioned_list.dart:221:60)
I/flutter (20336): #2      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4355:58)
I/flutter (20336): #3      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4201:5)
I/flutter (20336): #4      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (20336): #5      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter (20336): #6      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5445:14)
I/flutter (20336): #7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3194:14)
I/flutter (20336): #8      Element.updateChild (package:flutter/src/widgets/framework.dart:2988:12)
I/flutter (20336): #9      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4243:16)
I/flutter (20336): #10     Element.rebuild (package:flutter/src/widgets/framework.dart:3947:5)
I/flutter (20336): #11     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2432:33)
I/flutter (20336): #12     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:773:20)
I/flutter (20336): #13     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:283:5)
I/flutter (20336): #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1102:15)
I/flutter (20336): #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1041:9)
I/flutter (20336): #16     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:957:5)
I/flutter (20336): #20     _invoke (dart:ui/hooks.dart:259:10)
I/flutter (20336): #21     _drawFrame (dart:ui/hooks.dart:217:3)
I/flutter (20336): (elided 3 frames from package dart:async)
I/flutter (20336): 

Imho, PageStorage.of(context) can return null. What can we do about it? Should we just check for null and then use the default (that is already there in case readState returns null). Furthermore, writeState will fail in these cases.

wizlif commented 4 years ago

Any solution to this yet, it works fine on a StateFulWidget with FutureBuilder but crashes with the same error when the Widget is used in an Overlay

Zony-Zhao commented 4 years ago

Same as @wizlif, Async is not the problem. When I wrap the component in Hero widget. This problem appears. I use some hack to get it done for me temporarily. My fork here github repo. You should notice that I use null check in all the default PageStore thing . So you should not use this when you need to cache the scroll list position in a router.

AlexeyLihachev commented 3 years ago

Any updates? I encountered the same problem when using Hero animation

edwardaux commented 3 years ago

This problem still exists. As @wizlif mentions, I'm seeing it when using ScrollablePositionedList within an overlay.

Anyway, I'm pretty sure this project has been abandoned, but just in case someone else is still using it, I managed to hack around this bug by wrapping the ScrollablePositionedList in a PageStorage widget. Something like:

return PageStorage(
  bucket: PageStorageBucket(),
  child: ScrollablePositionedList(...)
);

This at least stops the crash