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
Related
I have a project that I had initially setup with Navigator and have since converted over to use BLoC Cubit.
When I use Navigator.of(context).pop(); to go back from a screen that was navigated to using context.read<AppCubits>().pageOneScreen(); I get a black screen instead of being directed back.
Does BloC Cubit's not allow or keep any history like Navigator does?
**** MORE DETAILS ****
I have a back button that would simply call
Navigator.of(context).pop()
and bring the user back to their previous page.
Now with Cubit, using the above doesn't work and returns a blank screen as there is no context stored.
So I have been searching for a solution to this but every one of them seems very verbose, is this type of feature not a part of Cubit or BLoC?
To navigate from page to page currently I am using
context.read<AppCubits>().pageOneScreen();
context.read<AppCubits>().pageTwoScreen();
This works as expected but I am looking for a solution to adding a call that will navigate back to the previous screen/state that was loaded. So if I was on pageTwoScreen, I could click back if I had come from pageOneScreen and have the state changed or at least be directed back to pageOneScreen.
Do I have to create a list and store the state value as a form of rudimentary history and then pop off the previous value and call something akin to
context.read<AppCubits>().historylistvalue()
I am new to flutter and I am currently working on a project that uses cameras internally.This screen has two image variables that need to receive an image from the camera and store them easily.
Right now the camera screen is closed like this:
onPressed: ()=> Navigator.pop(context,Image.file(File(imagePath)))
How do I change the Image variables from the previous screen?
I see you are passing a widget Image.file(File(imagePath)) to result parameter of Navigator.pop. So in this case, the code where you open the camera screen using Navigator.push , you can assign the return value of Navigator.push to some variable because the return type is Future<T>.
final imgWidget = await Navigator.push<Image>(...)
Flutter has no concept of different screens. Everything is Widgets and you have a Widget tree. Between straight Provider, Riverpod, Bloc and GetX there are a variety of different state management solutions that allow you to store information over the level of pages.
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.
Pardon me if this is a naive question but I am trying to figure out a way in Flutter to load different data when the user clicks a button. The way I currently see is routing the user to a new screen everytime but I am sure there would be a better approach without loading the whole screen everytime
You should use a Stateful Widget
I would create a Future Builder / ListView Builder. Depending on how you want it to behave you could have the ListView items clear before regenerating the list of items to show.
Future Builder you could create a function to return certain widgets depending on the request you make with the function.
When the user logs in, they get pushed to the homepage and their data is retrieved from database on initstate and then the widgets are built. However, i noticed for the widgets that are built without a streambuilder, where i manually retrieve data once, are duplicated if i navigate to another page and then come back. When I say duplicated I mean the exact same data is redundantly copied again using the same widget, so like a users' profile pic could show up twice. I am using a listview most of the time for these cases/
How do i avoid this and just built the widgets once so if the user goes back the page stays the same?