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

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.

Related

How to make a widget that its children can expand over its parent not in the same stack?

I have a TextFormField which as I type in it consults a cities database and the results are shown in a ListView just like a Google Maps search box. In a attempt to make the results to hover over other elements I've tried to add the listview in a stack with clip behavior set to "none" and various other configurations but I still couldn't managed to make the results hover over the page. I intend to use it for flutter web and there will be other elements which the search results cannot displace them to fit the query. I could access a parent Stack and pass the position of the TextFormField so I would know where to build the result list but this tinkering its not very straight forward so I was wondering if I could punch a hole in the parent layer from a child to print the result list.

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.

Flutter: A way to append incoming data to ListView instead of rebuilding entire thing?

Summary: Planning to show readonly data array in a ListView widget. Data comes in using a Provider. Is it possible to not rebuild the entire ListView every time, and just append a new Tile to it?
Details: The ListView data set coming in is similar to log entries--it's static and only the last item is coming in. Rebuilding the entire ListView every time seems a bit wasteful. Is it possible to have it in such a way that it would append new Tile widgets on incoming data instead?
Please provide some sample code or your code. For now from this question I'll suggest you to take a look on AnimatedList.

Is it necessary to optimize pageview with large amount of pages in Flutter?

I have a PageView with a large amount of pages in my app. Each of the pages is very complex with a PageView and its own pages.
I'm new to Flutter and my first hunch is that it needs optimization. So within the build() function of the outmost PageView I check the index and only make full page for current, previous and the next page(I guess swipe animation needs current page and the page next to it built ahead). For other pages I just give it an empty Container().
Is this necessary and the right thing to do? I felt this is an obvious optimization that the Flutter should do, but I can't find any related discussion online. Any suggestions are appreciated!
Flutter's PageView is lazily calling itemBuilder you provided. For an index it's called only when the page is really needed and you don't need to do anything more. But for further improvements you can make your view hierarchy simpler and prevent unnecessary build method calls. Here you can read more about Performance best practices.
No, it's not. ListView, GridView, and widgets alike are already optimized to build and render only what is needed in the moment. You get a hint of that in the PageView.builder description :
Creates a scrollable list that works page by page using widgets that
are created on demand
I think the difference between a regular PageView and a PageView.builder is that PageView initializes all children at startup, while PageView.builder are initialized lazily.

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

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