In which circumstances should you use listview instead of listview.builder? - flutter

Like the topic I was wondering of there are any circumstances one should use the regular ListView instead of the ListView.builder in flutter, like if there are few items in a list could the ListView give better performance?

ListView has actually four different ways to use it , But let discuss ListView and ListView.builder
ListView : It has a children
property that takes a collection of static widgets. A ListView takes a small number of other widgets and makes it scrollable. Why a “small number”? Because this is designed to be a static list, one that you, the developer, simply types into the build() method
by hand.
ListView.builder : ListView’s alternative constructor, ListView.builder receives two
parameters, an itemCount and an ItemBuilder property that is a
function. This makes the ListView lazy-loaded. The itemBuilder function
dynamically creates children widgets on demand. As the user scrolls close
to the bottom of the list, itemBuilder creates new items to be scrolled into
view. And when we scroll something far enough off the screen, it is paged
out of memory and disposed of. Pretty cool.
Reference : taken from Rap Payne's Beginning App Development with Flutter (Great Book for beginners! , not an affiliate link).
official documentation for ListView .

ListView is the most commonly used scrolling widget. It displays its children one after another in the scroll direction. So if you just want to show some widgets below earch other and you need to scroll them you use ListView.
ListView.builder is a way of constructing the list where children’s (Widgets) are built on demand. However, instead of returning a static widget, it calls a function which can be called multiple times (based on itemCount ) and it’s possible to return different widget at each call.

I guess the simple answer you were looking for is:
Use ListView.builder whenever you iterate over an array of 'similar' elements.
Use ListView when items in your list are completely different from one another.
Think of ListView as scrollable Column

Related

How can I create a Column widget with the functionality of an AnimatedListview in Flutter

I have a Column that I need to behave like an AnimatedList would: to animate the widgets when I add or remove them. The reason I can't use AnimatedList (as far as I can tell) is because my children are Cards wrapped in Flexible widgets. I only have 11 of these hard coded cards and I need them all to be visible should the user decide to have all of them "active".
Here's the desired effect but with terrible code:
I'm currently using the provider package to control the column's children. Adding in new Cards is simple enough, as they just run their animation on initState. Removing them involves a convoluted way of triggering the reverse animation, listening for when it finishes and then triggering a function that removes it from the Column.

Is building one large better than building small ~75-100 widgets

Consider the case that I have 3 adjacent list view with each list view having a sized widget of some height. Each list view has around 24 child items. I'm trying to scale the widget using onScaleUpdate in GestureDetector.
onScaleUpdate I want to change the height of each child item of all the 3 listviews.
Is rebuilding all child better or should I rebuild the whole widget?
As #Yeasin Sheikh pointed out, using ListView.builder is good because it builds only the needed children (the ones inside the screen and just a few ones outside as precaution). And as to actually answering your question, I'm no Flutter expert, but using ListView.builder I don't think it makes that much difference, Flutter is intelligent enough to solve this by itself.

Scroll Parent ListView when nested ListView.builder is updated

I have a ListView with, say, PostItems being generated with ListView.builder.
Each PostItem MAY have comments on it. So each PostItem has a nested ListView.builder to show comments for that PostItem.
User can add a Comment to the nested ListView. However, UX would like to scroll to that newly added Comment in the nested ListView. Nested Listview shrinkwrap is true, with NeverScrollable Physics. When nested Comment listview expandeds with new Comment, it's off screen and I want to scroll down to it.
ListView.builder -> PostItem -> Nested ListView.builder
However, my assumption is that I need to make the parent ListView scroll down but how do I know where to scroll to when it's the Nested listview that has grown in height.
I can't share code due to the project but any theological thoughts or ideas would be a great help!
Thanks
I can see that, based on your description, we can access the index of that particular ListView. Thus, I would suggest you to use this package to achieve what you want:
scroll_to_index

Difference between ListView.builder vs manual list?

I can also create lists by this code that I can with ListView.builder so what differentiate both?
for(Item in List) View(Item);
I am new to development and trying to understand this.
Is it good if I use above approach?
When items are limited(not more but few) you can use ListView widget, but for many items (like as thousands of items, also created on demand) - You should use ListView.builder.
The difference between of them is: When you use ListView, all the items are rendered on screen. But when you use ListView.builder, items are rendered on demand. Only the items are visible on the screen and few of above & bottom are rendered.
For more:
ListView
ListView.builder

What's the alternative to using a StreamBuilder inside of a column or row?

I am trying to build a page that shows a hierarchy of data. The top of the page is built by passed in data. But the dependencies need to query a db and post their results.
For example Team A has players. Under the heading of team A I want to show a list of players on the team. But if I try to add a streamBuilder as a part of screen, I get all sorts of nasty message about some part of the build process not having a height. Its seems that streamBuilder only works as the highest level widget.
So there must be an alternative for building those widgets, can anyone point me to an example of that being used?
An alternative to a StreamBuilder is FutureBuilder - where you can use a Future callback instead of a Stream to fetch a data snapshot for the children Widgets inside it.
However, if you're getting "unbounded height" errors, this is usually thrown by children Widgets with undefined height inside a ListView or Scrollable widget. You'll be needing to set a height for those List items to fix that error.