Flutter cache with hive
Recently when I want to get data from online I used Rest API and fetched the data from it,but the problem is every time it’s need healthy internet connection.And the thing is I can’t access it when I’m offline. So there I thought of using cache concept and save it in local storage.And so that data could be accessed without internet connection.
Libraries
In order to cache let’s go by step to step
Step➊ : Create model
Then after this run a command in teminal for building model_name.g.dart
flutter pub run build_runner build
Step ➋: Write services
Lets create API service in order to fetch data from online and decode it into JSON format
Now lets move on to next step for caching in this project we are using hive to cache data and lets write service for storing data into hive and retrieving data from hive
That’s it now lets use this service in view model
Step ➌: Now lets call this method
By using this services we can cache the rest api data
In my opinion hive is better for storing data in local and it is fast compared to shared preferences,Moore,sqflite.
For more details check this github repo: