0

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

4 Answers 4

1

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);
  }
Sign up to request clarification or add additional context in comments.

Comments

1

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)));

Comments

0

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

Comments

0

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.

Comments

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.