How to use nested json data in hive/shared_preference or any other in flutter

Viewed 123

I want to store a nested json data . In which each json object can be added/deleted by the user. Something like this:

{
  "folders": [
    {
      "folders": [
        {
          "folders": [
            {
              "folders": [],
              "links": []
            }
          ],
          "links": []
        },
        {
          "folders": [],
          "links": []
        }
      ],
      "links": []
    },
    {
      "folders": [],
      "links": []
    },
    {
      "folders": [],
      "links": []
    }
  ],
  "links": []
}

Like this in which each folders can be added or deleted by the user. One solution I found on internet is to use this json data as string by encoding it and store it in database(hive). I want to find a better solution if any?

1 Answers

For save data in sharedpreferences:

static void setJsonData(String? json) {
    _sharedPreferences?.setString('jsonData', json ?? '');
}

For fetch data from sharedpreferences:

static String? get getJsonData => _sharedPreferences?.getString('jsonData');

after getting this data, do parsing!

Enjoy!

Related