将本地数据保存在扑朔迷离上的列表中

发布于 2025-01-29 12:46:07 字数 965 浏览 1 评论 0原文

我有一个播放声音的应用程序。有声音列表,我将其显示到listView。因此,用户可以单击listTile并播放声音。我添加了一个“最喜欢的”部分。而且效果很好。但是,当用户关闭应用程序时,它会消失。我试图使用共享prefence,但我无法做到这一点。因为我需要保存整个列表,所以我不知道如何保存。

当我关闭应用程序时,我所有的“最喜欢”列表都消失了。

我将只分享所有代码以更好地理解:

main.dart

favourite.dart

这是图片解释:

“带2个最喜欢的声音的主页”

“重新启动后”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由如风 2025-02-05 12:46:07

由于保存歌曲的列表是可以从共享prefences中使用setStringList()方法的字符串列表。当您要保存列表时,请使用:

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

然后,当应用程序启动时,您可以使用以下方式加载歌曲:

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

然后您可以使用for-loop来获取已保存的索引,尽管我建议您仅尝试使用一个仅使用一个列表保存的歌曲。

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

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());
      }
    }
拒绝两难 2025-02-05 12:46:07

您可以使用sharon_preferences https://pub.dev/packages/ppackages/ppackages/ppackages/shared_preferences 如果您不介意使用FutureBuilder列出保存的列表。

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

}

//保存声音

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

读取

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

您可以在应用程序中使用未来的构建器窗口小部件在listView中填充声音,我建议将ListTile窗口小部件用作返回小部件
或者
您可以使用get_storage不需要以后的构建器小部件的库

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

保存

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

https://pub.dev/packages/packages/get_storage/exame/example/example

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

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>
我恋#小黄人 2025-02-05 12:46:07

您可以使用SQFlite软件包在本地存储列表项目。这是包裹:
https://pub.dev/packages/packages/sqflite

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文