2

I am new in Flutter, this is my first project in Flutter. I want to do a project that once data are loaded from API, they're cached in the device. Next time it can be loaded very fast even if my device is offline. I here that can using Dio package with dio cache manager package for caching server's json response. And then using cache image package to cache images. Anyone can give me some example how to write the code? Thanks in advance

6
  • why not save the data in sqlite db... it is local to user's device Commented Feb 5, 2020 at 9:02
  • you can use moor. Commented Feb 5, 2020 at 9:14
  • @DAMMAK thank you for your reply, you means that save my API response into sqlite? Sorry I am first time using flutter Commented Feb 5, 2020 at 9:50
  • @JohnJoe Thank you for your reply. My data is from API, also can use is package? Sorry I am first time using flutter Commented Feb 5, 2020 at 9:52
  • use shared preferences to store response Commented Feb 5, 2020 at 14:33

3 Answers 3

4

Another best way for caching in flutter is the use of the hive. enter image description hereAnd it retrieves data faster than Sqflite and Shared preferences

For example, you can check this GitHub repo:https://github.com/shashiben/Anime-details

This will show how to cache the rest API data into the hive and for the next time it will show data from the hive.

I think this answer helped you

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

1 Comment

my API data is in pagination. Hive also can save it?
0

Yeah i'd recommend the sqflite package for flutter, i've just figured out how to use it to solve this same issue! I learnt it using this YouTube video: https://youtu.be/1BwjNEKD8g8.

2 Comments

thank you for your reply, I want to do my project similar like facebook. When offline the post still maintain at there. If online can load new post. All data is from API. It also can use sqlite?
I don't know about that...I'm still new to coding! I just know that in my project i wanted to only save data locally, and sqflite worked great for that. Sorry i can't be of more help!
0

Try shared preferences https://pub.dev/packages/shared_preferences,

Just Decode your response as string and save to sharePreference, and Encode that String to Object when you need.

import 'package:shared_preferences/shared_preferences.dart'; 

//storing response as string
SharedPreferences sharedPref = await SharedPreferences.getInstance();
            Map decode_options = jsonDecode(jsonString);
            String user = jsonEncode(User.fromJson(decode_options));
            sharedPref.setString('user', user);//storing

//getting string and converting into Object
SharedPreferences sharedPref = await SharedPreferences.getInstance();
            Map userMap = jsonDecode(sharedPref.getString('user')); //retriving
            var user = User.fromJson(userMap);

    class User {
      final String name;
      final String age;

      User({this.name, this.age});

      factory User.fromJson(Map<String, dynamic> parsedJson) {
        return new User(
            name: parsedJson['name'] ?? "",
            age: parsedJson['age'] ?? "");
      }

      Map<String, dynamic> toJson() {
        return {
          "name": this.name,
          "age": this.age
        };
      }
    }

5 Comments

thank you for your reply, Can I know where the jsonString come from? Because I get the error with Undefined name 'jsonString'. Try correcting the name to one that is defined, or defining the name.
jsonstring is your response json from service call
I need to put SharedPreferences at model class code here or http response there?
use ShrPref in screen "eg: Loginpage.dart" class where you get the response
Sir, I put my code at above, I already add the SharedPreferences into code but when I offline it show loading only. Can you check is I put the SharedPreferences wrongly?

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.