3

I want to save the whole json response from api. I tried SQFLITE library to store but i cant able to achieve to store a complete json as it need to store in a table format. I'm very new to flutter. can any body suggest how can i achieve this. Below i'm attaching my sample json for your reference.

{
  "result": {
    "allitems": {
      "answered_status": 0,
      "list_items": [
        {
          "is_answered": false,
          "options": [
            {
              "image_url": "assets/images/orders/jersey.jpg",
              "description": "Jersey",
              "title": "Jersey",
              "price": 23
            },
            {
              "image_url": "assets/images/orders/bat.png",
              "description": "Bat",
              "title": "Bat",
              "price": 5
            },
          ]
        }
      ],
      "no_of_items": 12,
      "title": "ALL"
    }

  },
  "status_code": 200
}
3
  • 1
    By local database do you mean locally on a device? If that's the case then you can use SharedPreferences package to do that, but first you have to take the response into a Map<dynamic, dynamic> then encode it into JSON by using jsonEncode method. Commented Jul 9, 2020 at 14:38
  • @Abhishek Thanks for your response. If i use shared prefences, can i able to update any of the attributes in the above json when i get response from the user? I have to do CRUD operation on my data which is stored in local. Commented Jul 10, 2020 at 6:09
  • Yes, of course, I will compose an answer. Commented Jul 10, 2020 at 17:34

2 Answers 2

3

I was wrong about SharedPreferences in my comment. Turns out SharedPreferences doesn't support Map<dynamic, dynamic> and only up to List<String> yet.

So you can use a database management package sembast made by the same guy who made SQFLite.

You can get help with this link to convert JSON objects to Map and vice-versa.

EDIT -


You can do something like -

import 'package:sembast/sembast.dart';

Map<dynamic, dynamic> sampleMap;

// Skipping this part
sampleMap = // Retrive and convert JSON to Map

// A sample DB in the current directory
String dbPath = 'sample.db';
DatabaseFactory dbFactory = databaseFactoryIo;

// We use the database factory to open the database
Database db = await dbFactory.openDatabase(dbPath);

var store = intMapStoreFactory.store();

// Storing Map to DB
var key = await store.add(db, sampleMap);

// Retrieving values back
var record = await store.record(key).getSnapshot(db);

// From your provided sample JSON in question
var value = record["result"]["allitems"]["list_items"][0]["options"]["image_url"];

print(value);
// value = 'assets/images/orders/jersey.jpg'

Similarly, you can explore the documentation of the package for more data operations.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your valuable comments. tiil record i'm getting the response but i cant able to get the response in value(i.e, var value = record['result.allitems.list_items.options[0].image_url']; it returns null). can you help me in that?
You were able to store your response in Map and then store that to a local database ?
Yes i can able to store response in Map and then store that to a local database
Thanks a lot Abhishek i got output by changing var value = record["result"]["allitems"]["list_items"][0]["options"][index]["title"];
Edited. Good to see it works, it will help me also in the future.
1

Convert it to a string, you can store it in shared preference.

import 'dart:convert';
...
var s = json.encode(myMap);
// or var s = jsonEncode(myMap);

json.decode()/jsonDecode() makes a map from a string when you load it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.