I am building a home page where the user can replace default image. Whenever the user opens the application second time, I want that image user picked previously to show up and not the default image. How do I do this in dart/flutter?
I tried doing research, but was not able to find helpful articles.
3
Answers
You will need to persist between runs which image the user has selected. To do so, you can use the shared_preferences package.
When the user selects an image, set the image and also set a key using
shared_preferences
indicating which image was selected. The value of the key will likely be a string, and it could be — for example — a URL, a path on the local filesystem, or a path in the assets bundle. How exactly you choose to represent the user’s selection is up to you and depends on your specific use case.And, when your app loads, retrieve that key from
shared_preferences
and use the value to load the correct image for display.For this, it is necessary to save the last remaining image using the internal memory.
For example
Getx Storage
Hive
Remember the image sequence number every time the image is changed. This will help you show the images from the index immediately when the program is closed and reopened.
I gave a brief explanation here as general information.
You can save the image as files of specific path(getting the directory from
path_provider
, and then look for file at the same path the next time.Also you can use
image_picker
to select the image on iOS and Android.