我必须保存库存数据。我将它们组织为字符串列表,我该如何继续?读取和写入文件似乎不太合适,因为写入会覆盖(因此无法轻松更新),而读取会导致结构丢失。请帮助我!
最佳答案
- 将其添加到程序包的 pubspec.yaml 文件中:
dependencies:
shared_preferences: ^0.5.2
- 安装它,您可以从命令行安装软件包:
$ flutter packages get
- 导入它,现在在您的 Dart 代码中,您可以使用:
import 'package:shared_preferences/shared_preferences.dart';
以下是存储和获取字符串列表的方法:
String listKey = "listKey";
void storeStringList(List<String> list) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList(listKey, list);
}
Future<List<String>> getStringList() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.getStringList(listKey);
}
这是获取该列表的方法
getStringList((List<String> strList){
//strList is the string that you stored.
});
关于Flutter - 如何存储字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55641451/