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
[Suggestion] Provide a way to assert that a given Widget is a Sliver #94416
Comments
What is the exception that we throw if it is not a sliver? Would it be sufficient to just make that one better? |
@goderbauer My intention was to make this kind of code a little more type safe at compile-time:
The exception that we get for this code sample looks like this:
The message itself is clear to me, since I understand enough of RenderObjects. Perhaps we could include a fix in the exception message, similar to how other exceptions provide us with an alternate option. |
The problem is that any StatelessWidget/StatefulWidget can be used inside a CustomScrollView. So you could not only allow a list of List. I added a proposal here to support a ScrollView that accepts any type of widget |
Up until recently, I wasn't aware of the two "parallel worlds", slivers vs widgets, and it's been a shock, I must say.
@jamesblasco what do you mean, here? CustomScrollView does not accept box widgets, only slivers. The ScrollView widget that you propose in your twitter link is also what I have done in my code. Converting between slivers and regular "box" widgets is easy IMHO:
I think Flutter has a problem with redundancies between sliver and non-sliver widgets, eg. What is the deal-breaker here, justifying the very existence of slivers? I see there is one potential issue with |
Use case
Currently a
CustomScrollView
accepts a list of slivers.This list is currently a
List<Widget>
.However this creates a bit of confusion.
If I put a
Container
or any other regular widget in that list,I will get an exception since I used a Widget that is not a Sliver implementation.
Secondly, there is no easy option to assert that a given
Widget
is actually aSliver
implementation(aside from checking against all known Sliver types, i.e.
assert(widget is SliverList || widget is SliverGrid || ...)
).The current slivers are divided into implementations of
SingleChildRenderObjectWidget
andSliverMultiBoxAdaptorWidget
(and others?).There is no way to easily assert that a Widget is a Sliver, regardless of its actual runtime type.
Proposal
Make it possible to use an assert to check if a Widget is actually a Sliver.
I.e.
assert(myWidget is SliverWidget, "This widget must be a Sliver.")
Perhaps the different Sliver implementations implement a common (but empty) interface?
The text was updated successfully, but these errors were encountered: