Flutter - save json data local for a future list

980

You can save the JSON on string and when you have to use it, you can get the String and parse to a model.

Create this model:

// To parse this JSON data, do
//
//     final kitModel = kitModelFromMap(jsonString);

import 'dart:convert';

KitModel kitModelFromMap(String str) => KitModel.fromMap(json.decode(str));

String kitModelToMap(KitModel data) => json.encode(data.toMap());

class KitModel {
KitModel({
    this.the20210408,
    this.isLogged,
    this.senha,
    this.email,
    this.the20210408040319,
    this.token,
    this.id,
    this.content,
});

String the20210408;
bool isLogged;
String senha;
String email;
String the20210408040319;
String token;
int id;
String content;

factory KitModel.fromMap(Map<String, dynamic> json) => KitModel(
    the20210408: json["20210408"],
    isLogged: json["isLogged"],
    senha: json["senha"],
    email: json["email"],
    the20210408040319: json["20210408040319"],
    token: json["token"],
    id: json["id"],
    content: json["content"],
);

Map<String, dynamic> toMap() => {
    "20210408": the20210408,
    "isLogged": isLogged,
    "senha": senha,
    "email": email,
    "20210408040319": the20210408040319,
    "token": token,
    "id": id,
    "content": content,
    };
}

saving the kit ->

SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('content', content);

getting the kit ->

dart:convert


SharedPreferences prefs = await SharedPreferences.getInstance();
String KitJson = prefs.getString("content");

if (KitJson != null) {
  kit = KitModel.fromMap(json.decode(KitJson));
}

now you can use

print({kit.id, kit.content, kit.toke, kit.isLogged}); //And so on
Share:
980
Lucas Martini
Author by

Lucas Martini

I have 24 years old, male I work with web about 4 years and new to flutter/react native Web developer React native and Dart/Fluttet

Updated on December 29, 2022

Comments

  • Lucas Martini
    Lucas Martini over 1 year

    So, my question is, i want to save a json data localy so the user can see his saved projects.

      Future saveData() async {
        SharedPreferences kitSalvos = await SharedPreferences.getInstance();
    
        kitSalvos.setString('id', id);
        kitSalvos.setString('content', content);
      }
    

    The content is the json data.

    This is what i am doing, but, when i try to create a list of it, it brings other sharedPreferences.

      _getDataKits() async {
        SharedPreferences kitSalvos = await SharedPreferences.getInstance();
        print(kitSalvos.getKeys());
      }
    

    Output:

    {isLogged, senha, email, 20210408, 20210408040319, token, id, content}
    
    

    I want to bring only the saved projects (which is the ID and CONTENT) sharedpreferences...

    There is a way to make it separeted? So i can only get the ID and CONTENT...

    Also, make it one, like and object:

     kitsalvos = [ id: ID, content: CONTENT ];
    

    There will be more then 1 saved project, so i need to store all of it separeted...