Persist multi selected ListTiles with Shared Preferences - flutter

My problem is summarized perfectly with this solution:
Flutter ListTile enable multi-select onLongPress
My app is using shared Preferences and Provider to produce a todo list.
After restarting my app, Shared-Preferences returns/persists my list, but the SELECTED TILES go back to their initial state, UNSELECTED.
How can I persist those tiles with color/icon changes using Shared Preferences?
Using the solution above as a code example

Related

ShowSearch function rebuilds the whole widget tree on Flutter

I have a problem when i navigate to multiple pages and then show search.
More specifically, i have a product page with some details. At the bottom there is a list of similar products.
The flow is:
open many similar products (so I navigate to the same screen using Navigator pushNamed).
then, navigate from the las product page to the search page
tap on search bar, open search delegate using the showSearch function
My issue is that when i tap on the search bar, then the whole widget tree is rebuilded and my app is very heavy because rebuilds the previous product pages and everything else.
This happens due to the general rebuild on keyboard changes(i.e: showing and hiding Keyboard).
I recommend to have a heavy operation like loading data from backend should be held on initState in State with StatefullWidget.
Try to debug and understand why do you rebuild the tree. It seems to me, that you need to simplify a navigation flow.

Flutter Dropdown Button not saving state during Screen Navigation

Hi I am running into an issue where I am unable to save the state of the DropdownButton (the text shown in the Dropdown Button itself, which is the value that I have selected). I am using GetX for state navigation.
Due to the way my code has been structured, when I navigate from one screen to a second one, and wish to navigate back, I am utilising Get.to(First() instead of Get.back().
Here is my logic:
I will save the value the user has selected from the DropdownButton into SharedPreferences. This value will also be immediately shown to the user on the button itself due to the nature of the button.
When user navigates away (e.g. he is now on the second screen), and visits first screen again, I will load the selected value from SharedPreferences and construct the DropdownButton item such that this value will be shown at the top.
The issue I am facing:
Retrieving data from SharedPreferences is asynchronous, so I don't know how to construct the FirstScreen again while I am waiting for data to be retrieved from SharedPreferences. Could anyone help me out? Thank you!
Use GetStorage() instead of shared-preference . No Async issue
dependencies:
get_storage: ^2.0.3
use GetStorage through an instance or use directly GetStorage().read('key')
final box = GetStorage();
To write information you must use write :
box.write('quote', 'GetX is the best');
To read values you use read:
print(box.read('quote'));
// out: GetX is the best

How to prefetch data from api for 'ShowModalBottomSheet' even before it being opened in Flutter

Hi I'm trying to use ShowModalBottomSheet in my flutter App. But right now how it's being worked is the data which need to be displayed is being fetched only when ShowModalBottomSheet code executed and which creating a delay.
I want to know how do I prefetch the data beforhand using Future so that which can be used instantly when ShowModalBottomSheet triggered.
You could use and create a provider or bloc, and load the data, whenever the application's starts or the page is opened.
This way, the data will be available before the bottom sheet opens.

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.

Load list from api for dropdown only when tapped

I want a situation whereby when I tap a flutter dropdown, it pulls an updated list from api and render. I have tried using gestureDetector. So on tap it loads the list. But I don't know how to display the loaded list
In order words, how do I make flutter dopdownButtom item a future