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!
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.
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.
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:
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:
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.
Turns out using the NoSQL embedded database Sembast was the best option. Made by the same guy who made SQFLite!
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.
Shared preferences is an easy way to persist data in mobile apps. Flutter also has it: https://pub.dartlang.org/packages/shared_preferences
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With