flutter Hive重新启动后,在列表中获得一件项目的双重

发布于 2025-01-23 21:04:25 字数 1504 浏览 0 评论 0原文

我正在扑朔迷离中构建一个简单的预算应用程序,并且正在使用Hive进行本地数据存储。我有一个数周的清单,当应用程序启动时,它会从一个蜂巢盒中加载:

class WeekList extends ChangeNotifier {
  WeekList() {
    int boxLength = box.length;
    for (var i = 0; i < boxLength; i++) {
      listOfWeeks.add(box.getAt(i));
    }
  }

我看到的问题是如果我开始新的一周,然后快速重新启动该应用程序,上一周中有两个。添加新的一周运行以下代码:

  void addWeek({
    required double budget,
  }) {
    Week newWeek = Week(
      budget: budget,
    );

    listOfWeeks.add(newWeek);
    box.add(newWeek);
    notifyListeners();
  }

奇怪的部分是如果我添加新的一周,并在一周内编辑一个

值一周看起来像这样:

  //updateSundayValue
  void updateSunday(
      {required double newValue, required int index, required Week week}) {
    listOfWeeks[index].sunday = twoDecimalPlaces(newValue, 2);
    listOfWeeks[index].sundayDisplayValue =
        twoDecimalPlaces(newValue, 2).toString();
    box.putAt(index, week);
    notifyListeners();
  }

对我来说很奇怪,如果我不编辑新的一周,我刚刚添加了上一周的上一周。..

更新1:更多故障排除

i i通过在添加一周之前和之后的代码中添加一堆打印语句,进行了更多的故障排除。

在下面的此代码中,我检查了我的蜂巢框,并将所有内容加载到周列表中,并打印出周日的值。

然后,我运行addweek()函数,然后将所有内容从Hive Box重新加载到另一个数周的列表中,然后将其放置。

在节省之前,我有两个星期,周日值是1和2。i

然后addweek(),默认值为0。但是,当我看到打印说明的输出时,应付前一周,打印出1和2和2,我希望它是1和2和0。

“在此处输入图像说明”

I am building a simple budgeting app in flutter and I am using Hive for local data storage. I have a list of weeks, that gets loaded from a hive box when the app starts:

class WeekList extends ChangeNotifier {
  WeekList() {
    int boxLength = box.length;
    for (var i = 0; i < boxLength; i++) {
      listOfWeeks.add(box.getAt(i));
    }
  }

The problem I am seeing is if I start a new week, and then quickly restart the app it loads with two of the last week. Adding a new week runs this code:

  void addWeek({
    required double budget,
  }) {
    Week newWeek = Week(
      budget: budget,
    );

    listOfWeeks.add(newWeek);
    box.add(newWeek);
    notifyListeners();
  }

The odd part is if I add a new week, and edit a value in the week then when I restart the app it does not create double of the last week on load...

Updating a value in the week looks like this:

  //updateSundayValue
  void updateSunday(
      {required double newValue, required int index, required Week week}) {
    listOfWeeks[index].sunday = twoDecimalPlaces(newValue, 2);
    listOfWeeks[index].sundayDisplayValue =
        twoDecimalPlaces(newValue, 2).toString();
    box.putAt(index, week);
    notifyListeners();
  }

its odd to me that it will repeat the last week added to the list on load if I dont edit the new week I just added....

Update 1: More troubleshooting

I am conducting some more troubleshooting, by adding a bunch of print statements to the code before and after the add week.

In this code below I check my hive box and load all its contents into a list of weeks and print out the value for sunday.

I then run the addWeek() function and afterwards I reload all the contents from the hive box into another list of weeks and out put it.

Before the save I have two weeks, whos sunday values are 1 and 2.

I then addWeek() which should default to a sunday value of 0. But when I see the output of the print statement it somehow copes the week before it and prints out 1 and 2 and 2 and I expect it to be 1 and 2 and 0.

enter image description here

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

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

发布评论

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

评论(1

喵星人汪星人 2025-01-30 21:04:25

使用box.put()而不是box.add(),box.add()将继续添加数据并创建列表。使用box.put(键,'your string');
它将起作用

use box.put() instead of box.add(), box.add() will keep adding the data and create a list. use box.put(key,'Your string');
it will work

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