Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to persistently store JSON data directly in Flutter for querying and offline use?

What is the best way to store JSON data pulled from a RESTful API in a Flutter app persistently (for offline use)?

Is there an easier way to do this than sqflite?

Thanks!

like image 222
Nomnom Avatar asked Apr 30 '19 13:04

Nomnom


People also ask

How do I store JSON data offline in flutter?

To persist JSON for Offline work a good option is to use couchbase lite, because it's a NoSql DB. Which means you can perform queries over it. You can use fluttercouch to work with flutter.

How to persist JSON for offline mode in flutter?

To persist JSON for Offline work a good option is to use couchbase lite, because it's a NoSql DB. Which means you can perform queries over it. You can use fluttercouch to work with flutter. Regarding to your question about saving the information when the app it's about to close? you better save it when it goes to the foreground.

How to create a sample JSON file in flutter?

Create a new Flutter project: 2. Create a folder called assets (the name doesn’t matter) in the root directory of your project then copy the sample.json file into it: 3. Declare the json file in the assets section in your pubspec.yaml file:

How are reviews stored in flutter?

These are stored as a list of maps for the reviews key, and each review is a valid JSON fragment in itself. When a JSON response is sent over the network, the entire payload is encoded as a string. But inside our Flutter apps we don't want to manually extract the data from a string:

How to handle JSON data from the Internet?

Since most data are JSON typed when you fetch it from the internet, we will need to handle it because JSON data are not automatically parsed. To do so we will use json_serializable plugin, which will do the dirty jobs for us.


3 Answers

Turns out using the NoSQL embedded database Sembast was the best option. Made by the same guy who made SQFLite!

like image 93
Nomnom Avatar answered Oct 06 '22 01:10

Nomnom


Shared preferences is not a good solution for what you're looking to accomplish.

I would read and write a local file: https://flutter.dev/docs/cookbook/persistence/reading-writing-files

And I would use an observer to constantly save changes.

like image 29
Leoog Avatar answered Oct 06 '22 00:10

Leoog


Shared preferences is an easy way to persist data in mobile apps. Flutter also has it: https://pub.dartlang.org/packages/shared_preferences

like image 29
Gazihan Alankus Avatar answered Oct 06 '22 01:10

Gazihan Alankus