Moisés Ochoa
Moisés Ochoa

Reputation: 93

How to save list with sharedPreference in Flutter

I have a problem saving a list with sharedPreference in Flutter, I have an array like this:

enter image description here

Well, to save the list "TodosEventos", apply the following code

SharedPreferences prefs = await SharedPreferences.getInstance(); 

await prefs.setStringList('events',todosEventos);

but I have this problem when running

enter image description here

Upvotes: 0

Views: 669

Answers (4)

srikanth7785
srikanth7785

Reputation: 1512

Shared Preferences is basically used to save just small values, typically, boolean kind of values.

So, if you want to do something like "ToDoList" kind of thing, there is a good package available on pub.dev called pref_dessert which makes the task of saving such data. Or you can also make Offline Database on the user's device by using sqflite package.

I would suggest you to use pref_dessert, if it's going to have small amount of data.. however, if it's complexed sqflite would be good to go.

Upvotes: 0

Navaneeth P
Navaneeth P

Reputation: 1561

SharedPrefrences allows you to store a json.

So What I suggest is add a toMap() method to Event class:

Map<String, dynamic> toMap() {
  return {
    'imagePath': imagePath,
    'title': title,
    'eventId': eventId,
    ...
  };
}

And a function that create a map of all the events:

Map<String, dynamic> todoEventosMap() {
  Map<String, dynamci> map;
  todoEventos.forEach((event) {
    // eventId should be unique
    map[event.eventId] = event.toMap();
  });
  return map;
}

Then u can encode the map to json and save.

import 'dart:convert';

await prefs.setString('events', json.encode(todosEventosMap()));

To get back the list from sharedPreferences:

You can add a convenience factory method for Event:

factory Event.fromMap(Map<String,dynamic> map) {
  return Event(
    imagePath = map['imagePath'],
    title = map['title'],
    eventId = map['eventId'],
    ...
  );
}

When creating todoEventos pass the map to the factory:

List<Event> todoEventos;

var events = json.decode(await prefs.getString('events')) as Map<String, dynamic>;

events.forEach((eventId, eventMap) => todoEventos.add(Event.fromMap(eventMap)));

Upvotes: 1

Adnan Kazi
Adnan Kazi

Reputation: 84

No but indirectly yes Main Logic is you just need to encode your List while storing and decode while you are retrieving.

SharedPreferences prefs = await SharedPreferences.getInstance(); 

 await prefs.setStringList('events',generateList(todosEventos));

Here you can generate String from list

  generateList(todosEventos) {
    Map list;
    todoEventos.foreach((element) {
      list[element.id] = element.toMap();
    });
    return json.encode(list);
  }

Upvotes: 1

Mariusz Brona
Mariusz Brona

Reputation: 1609

Event class is not a String. What I would propose is you should save the List as a JSON:

import 'dart:convert' show json;
import 'package:shared_preferences/shared_preferences.dart';

void setList(String key, List<dynamic> value) async {
  await setString(key, json.encode(value));
}


setList('key', todosEventos);

The same problem is discussed here: Can i store a List<dynamic> with shared preference

Upvotes: 0

Related Questions