OnurParlak
OnurParlak

Reputation: 97

Saving Local Data for a list on Flutter

I have an app that plays a sound. There is a list of sounds and I show that to an listview. So user can click a listtile and plays a sound. And I added a "Favourite" section. And it works fine. But when user closes the app it goes away. I tried to use SharedPrefences but I couldnt manage to do it. Because I need to save the whole list and I dont know how.

When I close the app, all my 'favourite' list goes away.

I will just share all the codes for better understanding:

Main.dart
Favourite.dart

Here is the explaining with the pictures:

Main page with 2 favourite sound

Favourite Page

After the restart

Upvotes: 3

Views: 3914

Answers (3)

Walulya francis
Walulya francis

Reputation: 573

you can either use shared_preferences https://pub.dev/packages/shared_preferences if you don't mind using FutureBuilder to list saved list.

import 'dart:convert' as convert;
class SoundsService{
late SharedPreferences _prefs;

Future<void> saveSounds(List<Map> soundList) async{
 prefs = await SharedPreferences.getInstance();
 String soundListString =  convert.jsonEncode(soundList);
 await prefs.setString('sound_list',soundListString);
}

Future<List<Map>> getSounds() async {
 prefs = await SharedPreferences.getInstance();
 String soundListString = _prefs.getString("sound_list") ?? "[]";
  return  convert.jsonDecode(soundListString) as List<Map>;
}

}

//saving sound

final SoundsService sService = SoundsService();
  List<Map> exampleSoundList = [
    {"title": "dehuyguyuuh", "subtitle": "khiuhiuhui"},
    {"title": "dehuyguyuuh", "subtitle": "khiuhiuhui"},
    {"title": "dehuyguyuuh", "subtitle": "khiuhiuhui"},
    {"title": "dehuyguyuuh", "subtitle": "khiuhiuhui"},
    ];
    sService.saveSounds(exampleSoundList);

Reading

List<Map> exampleSoundList = await sService.getSounds();

you can use Future Builder widget in your app to populate sounds in a Listview, i recommend using ListTile widget as a return widget Or you can use get_storage library which doesn't require Future builder widget https://pub.dev/packages/get_storage/example

import 'package:get_storage/get_storage.dart';
main() async {
  await GetStorage.init();
  runApp(App());
}

Saving list

 final box = GetStorage();
 await box.write('sound_list', convert.jsonEncode(soundList));

Reading list

   final box = GetStorage();
    List<Map> exampleSoundList = convert.jsonDecode(box.read('sound_list') ?? 
    "[]") as List<Map>

Upvotes: 1

Nick
Nick

Reputation: 81

You can use the SQFlite package to store your List items locally. Here is the package: https://pub.dev/packages/sqflite

Upvotes: 0

SlowDeepCoder
SlowDeepCoder

Reputation: 882

Since the list of saved songs is a list of Strings you can use setStringList() method from SharedPrefences. When you want to save the list, use:

final prefs = await SharedPreferences.getInstance();
await prefs.setStringList('savedSounds', savedSounds);

Then when the application starts you load the songs by using:

final prefs = await SharedPreferences.getInstance();
savedSounds = prefs.getStringList('savedSounds') ?? [];

Then you can use a for-loop to also get the saved indexes, although I would recommend you to try only using one list of the saved songs.

for(int i = 0; i < savedSounds.length; i++){
      if(soundList[i] == savedSounds[i]){
        savedIndex.add(i.toString());
      }
    }

Upvotes: 4

Related Questions