2

My app uses both shared_preferences and writing to a local json file (getting path using path_provider) for the data that it uses. I recently started testing it (starting 1/1/22) with a larger group and some people are having an issue where the data is lost when they close the app and reopen it. We're testing on both Android and iOS. The first user to have the issue was on iOS and has had it from the beginning. All the other users ran into the problem a couple of days ago (1/12/22) and is a mix of mostly iOS and 1 Android user.

The data persists while the app is running since it's consistent while the user navigates around. I haven't been able to replicate the problem myself and I haven't been able to find out what the issue in my research. Any thoughts of what may be going on?

I found a similar question that mentioned that path_provider provided different paths for storage, but that doesn't seem to be my issue here since the first piece of data that gets called is the shared_preferences data and that is also getting lost.

  • You may need to add some temporary code to the app to report to the API any issues or to pull up a dialog that can be screenshot-ed and send to you to provid details from the specific device. Without your code and without debugging it's very difficult to even determine whether it's the shared_preferences or the json file or both that is having the problem. Let alone why. In my experience, shared_preferences has worked really well for what I use it for. – daddygames Jan 14 '22 at 19:49
  • You haven't provided enough information to determine exactly why/how your shared preferences are getting "lost". Look here: [Flutter SharedPreference do not persist](https://stackoverflow.com/questions/54523760/flutter-sharedpreference-do-not-persist) or here [Flutter: The 4 Ways to Store Data Locally (That You're Going to Need)](https://levelup.gitconnected.com/the-4-ways-to-store-data-locally-in-your-flutter-app-that-youre-going-to-need-abdafa991ae3) for useful insights/potential alternatives. – paulsm4 Jan 17 '22 at 17:54

1 Answers1

2

I did some troubleshooting and we figured out that the devices (mostly iOS) were deleting the data point from shared_preferences. I migrated that point of data to a json file and the issue disappeared. It does say in the documentation not to use shared_preferences for any critical piece of data and now I see why. It worked initially, but had issues later for a lot of people, so I'm probably not going to use it in the future.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • so did u migrate to json file and saved the json file inside the shared_preference? or did you use another local storage tool ? because im facing the same issue here – Ali Nov 22 '22 at 19:16
  • I basically followed [this](https://docs.flutter.dev/cookbook/persistence/reading-writing-files). It is a good guide with reading and writing files. Json has a couple of other nuances, but I'm assuming you know how to work with json. – Harrison Pope Nov 23 '22 at 22:16