3

I am pretty new to flutter and currently, I am writing an app that needs to store key-value pairs to disk and read it each time the app is open.

I based on this document, using shared preference package, and it works fine. However, debugging is not easy, I am used to web development, and the similar idea, localStorage can be easily accessed and edited in run time using almost any browser such as Chrome, this feature makes it easy to develop and debug.

So my question is that is there some equivalence in Flutter development that I can easily edit the shared preference storage? One of the most needed functionality is to clear everything in the shared storage, so I can repeat running a clean program and debug.

Right now I can only do this by writing a functional code like preferences.clear() and running it once, which is a pain.

I am using VSCode + Android AVD for Flutter development.

Thank you!

beedrill
  • 344
  • 3
  • 17
  • Seems that there is no any top level api to remove shared preferences. You could add logic for checking debug mode or release and depend on modes, adding logic. However, it could be nice if you'll find solution. I'll follow this post. – Atamyrat Babayev Dec 22 '21 at 05:36
  • Oh, do you need to clear data of shared preferences on app restart? If so, https://stackoverflow.com/a/62633774/10084055 – Atamyrat Babayev Dec 22 '21 at 05:39
  • @AtamyratBabayev Hi, I am looking for debugging tool, something I could just clear the buffer and do a fresh start of the program. Right now, I am writing some temporary code in the main file just to clear buffer and then comment it out. That's obviously not the 'right' way. – beedrill Dec 22 '21 at 06:18
  • As I said, there is no any top level api to remove shared preferences. What I suggest to do is to create singleton of "pseudo" shared preferences, which will keep data till application will die or will become hot reloaded. Anyway, my choice is to clear data on app started. – Atamyrat Babayev Dec 22 '21 at 11:37
  • Oh, sorry, my use case is in mobile development. – Atamyrat Babayev Dec 22 '21 at 11:40
  • @AtamyratBabayev That sounds unfortunate. I am currently also in the main function do a pref.clear() whenever I need a fresh restart, and comment it out after. It seems not the right way though, considering this is such a common need for development. – beedrill Dec 22 '21 at 17:46
  • seems that you need to create an issue related to this feature in github. – Atamyrat Babayev Dec 23 '21 at 05:15

2 Answers2

3

I found a work-around, by directly modify the files in Android virtual machine: Go to Android View -> Tool Windows -> device file explorer.

Here you will see all the files on the virtual phone device.

Navigate to your app folder, normally in data/data/com.example.yourprojectname

In shared_prefs folder, there is an XML file containing all the local key-value pairs, you can directly modify it, or delete it here.

PS: At the current stage, if the Flutter app has heavy features based on local (SQLite and shared preference), Android Studio is a much better-developing tool than VSCode, for much better virtual device inspection.

beedrill
  • 344
  • 3
  • 17
0

So my question is that is there some equivalence in Flutter development that I can easily edit the shared preference storage?

Yes you can edit SharedPreferences values by the key which you set,

prefs.Set{anytype}(key,value); // will be used to set and update if key not found it will create else if found it will update to the same key.
updateSharedKeyVal(String prefKey, String prefValue) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString(prefKey, prefValue);
    // you can save or set value to persistent storage in the background
    //prefs.setInt(prefKey, prefValue);
    //prefs.setBool(prefKey, prefValue);
    //prefs.setDouble(prefKey, prefValue);
}

updateSharedKeyVal("my_key_name","my_updated_value");

To Clear,

clearSharedPreference() async {
   SharedPreferences prefs = await SharedPreferences.getInstance();
   prefs.clear();
}
Jai Techie
  • 1,657
  • 3
  • 14
  • 36
  • That is not what I am looking for, I am looking for debugging tool. Something I can clear the buffer, without writing a temporary code to do that. And yes, currently I am doing something similar by placing a temporary code in the main function just to clear the buffer, but obviously this is a very troublesome way. I'm looking for something like Chrome inspect in web development. – beedrill Dec 22 '21 at 06:16