How to update list view without refreshing in Flutter - flutter

How can it update a list view elements without doing pull-to-refresh
If I'm adding an element in a dialog window or from an Api, how to add/update the list view elements automatically.
Should I use StreamBuilder?

Use state management pub like Getx, or something with adlistener.

Yes you can use stream builder and you can also use setstate
setState(() { });

StreamBuilder Widget builds itself based on the latest snapshot of interaction with a Stream. Thus the state of the app changes whenever new elements are added in the list as long as the connection status is active.
You can also use the combination of ListView Builder and setState method, though the state of the app needs to change whenever a new element is added. Earlier StreamBuilder widget was updating the state of the app. Various methods like adding a button to the screen, which can help the user manually change the state; can be used depending on the use case.
You can read more about it from the official docs: setState method
StreamBuilder

This answer is not exactly about updating listView, but updating a widget(PiChart). As listview is also a widget, the implementation will be the same to update without refreshing.
In this method, I am using streamBuilder to update the widget. so need not refresh the app to update the data.
https://stackoverflow.com/a/69800324/13577765

Related

Trigger rebuild of other widgets after FutureBuilder finishes

I have a page with two main widgets: a futurebuilder that contains a list of elements taken from an api and an appbar.
The appbar content (a row of days) is grayed out depending on the result of the api (which returns a list of dates). When I interact with the appbar, that has a button to update the data from the api, it triggers a rebuild (with setstate I update the future with the api call). However, when FutureBuilder finishes and updates the state with the result, the appbar does not refresh. How can I force an update? I tried calling setstate from inside the futurebuilder but it doesn't work.
Thank you.

Communication between Flutter widgets

Flutter widget update pattern is a little bit confusing when coming from "old/classic" win32.
For instance :
I have a widget "button" => I click on it => I update a cell in a widget "datatable".
With classic API (VCL, Net Forms, ...) I get the address of button (by name, id, ...) and call directly
datatable_address.cell[x,y] = new_value;
I understand that I have to use setState() but do I need to create an "event" in Datatable (to run its setState()) and fire up this event from my button ?
(BloC seems pretty close to Qt signals)
The difference is composition vs aggregation. The widgets themselves are immutable so you can't change the value inside a widget.
What you can do is create a new object with latest values. Flutter framework will take care of the rest. For example,
when you do setState in parent widget of both button as well as datatable, you build method of said widget is called. Now, you will create a new datatable object with updated cell values. Internally, flutter will handle this.
You can only do setState in State class which is not immutable which is linked to Stateful widget which themselves are immutable.
This architecture comes with its own set of problems like if you need to update some UI based on some button press, you will have to have the logic inside the common widget of both the affected widget and button. That's where Stream/Bloc comes into picture.

Flutter Listview items not rendering

What I do is fetching items from Firebase then saving them in a list and then I display them in a ListView. The Problem that I have now is that I check if the list has Items and if so it displays my ListView with the Items and if there are no items it displays a Text but even but even if there are items the text gets displayed but after pressing hot reload the items get shown.
My guess was that there is a Problem with the State of the Widgets overall because stuff like SetState has no effect or a Refresh Indicator
Is there a reason why the StreamBuilder is following your masterListStart().asStream as opposed to the masterList stream? I'm not saying it's wrong, its just not immediately obvious.
Either way, it's not rebuilding automatically because the only part of your displayStory method that is reactive to the stream doesn't get built until after the non empty list condition is met. Without implementing a reactive state management solution there's nothing in your code that automatically notifies any listeners that the state of the storysList has changed, which means nothing is triggering a rebuild. Which is why you have to hot reload to see the changes.
Without me getting too deep into your code, try returning the StreamBuilder at the top level of the displayStory method and putting all the conditionals inside its its builder method.

How to prevent Stateful Widget stored in List to be disposed?

Let me start by explaining the problem. I have several buttons which are created based on data I'm getting from a server. On each button click I need to create and display a widget which will present me some data (this widget is also built dynamically). The state of this widget has to be preserved during the lifetime of the app. (for example if I click another button and show a different widget, I need to be able to click on the first button and show the first widget in its preserved state). Number of buttons can also be changed during the app lifetime.
I tried using IndexedStack to achieve this, but when the number of buttons is changed I need to add "pages" to IndexedStack, therefore I need to recreate a new IndexedStack which will have some of my old widgets, so I pull widgets from a List or create new ones if needed. This works great, except Flutter calls dispose() method on my widgets which are stored in the list. I tried using the AutomaticKeepAliveClientMixIn but it didn't help.
I'm guessing this has something to do that the widgets get detached from the parent, but when I reattach them to new parent (new Indexed stack) Flutter doesn't figure out this properly.
Any ideas?
Try to store data to local storage or sqlite for persistence data.

Right way or difference between using setState and streamBuilder in flutter

I want to know, when we should should use streamBuilder and when we should use setState method. Should we use only one way in the project for one code style or we can use are both ways.
For example, we have radio buttons. We can handle it and to redraw after changing radio button via streamBuilder and via setState, but what way is more useful?