【发布时间】:2026-02-13 18:30:01
【问题描述】:
我正在尝试将某些数据保存在运行我的应用程序的设备上。保存数据的代码如下:
Future<File> writeContent() async {
final file = await _localFile;
// Write the file
return file. ...;
}
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
print(directory.path);
return directory.path;
}
Future<File> get _localFile async {
final path = await _localPath;
return File('$path/UserData.txt');
}
我的问题是我不只是想保存一个字符串,我想保存一个列表(数组)到电话。有没有办法这样做?
一定有你看到...的东西。
提前感谢您的回答!
【问题讨论】:
-
您可以使用pub.dev/packages/pref_dessert (
pref_dessert) 来实现这一点..您可以创建一个自定义类,其中包含您想要存储的所有详细信息..以及每次您想要存储的东西。 .为其创建一个与该对象相同的对象..类似地..每当您需要存储的详细信息时...您可以获取该对象并从中读取详细信息 -
您可以简单地将数组编码为 JSON 字符串,保存该字符串,然后在检索它时将其解码回数组。您可以在此处查看使用 SharedPreferences 执行此操作的示例:youtube.com/watch?v=oPrpk_JiGvc