-1

How could you store something like that even when the app is closed in Flutter:

Class myList {
    String id;
    List<Item> list;
}

Class Item{
    //Many property’s
}

I thought maybe I could do that with "sqflite", a flutter dependency, but I have no Idea how I could store there this List<Item>. Do you have any idea?

Btw: I need multiple of these "myList" instances.

James Z
  • 12,209
  • 10
  • 24
  • 44
Boothosh81
  • 387
  • 1
  • 5
  • 24

2 Answers2

1

You can store it using sqflite,hive or shared preferences.Try to save it under flutter shared preferences. These are two methods that you can use.

Using shared preferences 1)First create a shared preference instance

SharedPreferences prefs = await SharedPreferences.getInstance();

Then save the relevant data type.here you need to put a object list so add the list and convert it to the String using jsonEncode.

Map<String,List<Item>> map={mylist.id: mylist.list};
prefs.setString("itemList", json.encode(map));

Then you can retrieve data like this

Map<String,List<Item>> map=json.decode(prefs.getString("itemList")).asMap();

2)Using Hive First Create the hive databse.you can put any name here

var box = Hive.box('myBox');

Then add the object in to that database

var myList = myList()
..id = your id here
..list = add list here;

var name = box.add(myList);

You can get anywhere this data list.

print(box.getAt(0));
  • Is there any reason why people say "don't store large amounts of data in shared preferences"? I mean the option with shared preferences looks easier, but I have to store probably almost 10 of these Lists with contains 3-6 of my Items. Is that a problem for shared preferences ? – Boothosh81 Jul 15 '21 at 11:39
  • yes consider the data amount when using the shared preferences. Think Hive is the best solution.See the documentation in pub.dev for hive. – Dineth Prabashwara Jul 15 '21 at 12:21
  • Okay, that means you would say up to 60 of these Items (wich all have 5 String or Int propertys) is too much for shared preferences, or at least hive would be much better? – Boothosh81 Jul 15 '21 at 12:38
  • It's not get error.Using large amount data in shared preference get you some performance issues. Hive would be much better. – Dineth Prabashwara Jul 15 '21 at 12:46
1

You can try hive package. It is a lightweight local storage passage and it is a good idea to use hive because it has better benchmarks for reading and writes. You can search for the tutorial for the hive. or just read the documentation.

Ayush Rawat
  • 152
  • 7