所以,我发现使用Get_storage解决了这个问题,并得益于有关该主题的一些很好的解释。我成功地使用getx和Provider包在添加新内容并启动应用程序时读取数据来保存数据(这是我想要的行为)。话虽如此,我正在尝试从内存中删除数据时遇到困难。
背景
该项目是一个待办事项列表应用程序,前端运作良好,但当涉及到存储时就变得更加复杂。问题在于我非常新手Flutter和移动开发,在这方面我接受了一些帮助,但这种东西仍然在我的脑海中模糊不清,我无法使用相同的逻辑删除数据。当我像文档中所说的那样调用box.Remove('key')时,我的整个列表都被删除了。我不知道为什么会发生这种情况。
因此,我想通过阅读更多的解释来更好地理解它,我知道Shared Preferences对于处理这种情况非常重要,但如果使用Get_storage解决方案也可以让我感到舒适,因为我更熟悉它。
代码:
我在另一个文件中利用Provider的帮助在listView中调用这些列表。
List<Task> _tasks = [
Task(
name: "Title",
description: "Description",
),
向我的列表视图中添加任务 - -
void add(String newTitle, newDesc) {
final task = Task(name: newTitle, description: newDesc);
_tasks.add(task);
notifyListeners();
}
这里是从ListView中删除任务的方法:
void removeTasks(Task task) {
_tasks.remove(task);
notifyListeners();
我尝试实现一个逻辑来写入和读取数据,它成功了。但是我也试着使用removeTasks方法通过调用box.Remove('tasks') 从存储中删除它 ('tasks'是传递给写入和读取方法的键)。 它将所有东西都从内存中删除了,因此我的listview变成空的了。
由于我经验不够丰富,我查阅了文档,并能理解一些SharedPreferences解释(与got_storage相同),但是当我尝试将其应用到我的代码时,我遇到了困难。
我非常感激任何使用get_storage或shared preferences来解决这个问题的帮助。
这是我调用删除的地方:
// bool variables that control the state of the screen
// since i can change it to show done tasks or on goind tasks
// dont mind that, i think its irrelevant to the problem.
//
bool isActiveDoing = true;
bool isActiveDone = false;
List finalArray = []; //it will store the tasks
class TaskList extends StatefulWidget {
@override
_TaskListState createState() => _TaskListState();
}
class _TaskListState extends State<TaskList> {
@override
Widget build(BuildContext context) {
//dont mind the if else as well, its not part of the problem
//just using it to handle the state of the screen
if (isActiveDoing) {
finalArray = Provider.of<TasksFunctions>(context).tasks;
}
//TasksFunctions is a class with methods on regards to the storage
//it contains add tasks, remove, etc... i'm using provider to
//link those to the screens with the notifyListeners
if (isActiveDone) {
finalArray = Provider.of<TasksFunctions>(context).doneTasks;
}
//now here is where i call the class tha has the deletion method
return Consumer<TasksFunctions>(
builder: (context, tasksFunctions, child) {
return ListView.builder(
//list view tha has all the tasks
itemCount: finalArray.length,
itemBuilder: (context, index) {
final task = finalArray[index];
//using the slidableWidget to wrap the deletion method
return SlidableWidget(
onDismissed: (action) {
if (isActiveDoing) {
Provider.of<TasksFunctions>(context, listen: false)
.removeTask(task);
//so here is where i'm deleting those tasks, calling that method
//listed up on this post
}
if (isActiveDone {
Provider.of<TasksFunctions>(context, listen: false)
.removeDone(task);
}
},
);
},
);
},
);
}
}
我花了一些时间来翻译这段代码,但我认为它并不符合Flutter的良好实践原则。
我还尝试过调用storageList.remove(task);然后用box.write('tasks', storageList)重写它,但是没有从内存中删除任何内容(也许是因为我没有遍历整个storageList以查找正确的索引位置吧)。
Unique Keys
,你可以将它们作为你的Task
对象的一部分,然后当你删除某些东西时,可以遍历列表并找到匹配的键,这样只会删除那个。将它们转换为字符串,它们也可以成为映射存储的键。这是众多可能的解决方案之一。https://api.flutter.dev/flutter/widgets/UniqueKey-class.html - Loren.AUniqueKey
的话就不需要索引了。因为在我之前的示例中,索引只是一个快速生成唯一映射键的hack。请查看更新后的答案。 - Loren.A