Flutter/Hive:对内存中列表的更改会跨屏幕保留

Flutter/Hive: Changes to List in Memory Persist Across Screens

提问人:sifat ullah shah 提问时间:11/17/2023 最后编辑:sifat ullah shah 更新时间:11/17/2023 访问量:41

问:

我在 Flutter 应用程序中遇到了一个问题,我在第一个屏幕中从 Hive 数据库中检索记录,对内存中的列表进行更改,关闭第一个屏幕而不将数据保存到 Hive 数据库,然后打开第二个屏幕。在第二个屏幕中,当我从 Hive 数据库中检索新记录时,我对第一个屏幕中的列表所做的更改仍然存在,即使我没有将它们保存到 Hive。

这里从数据库检索到的记录(意大利面基础成分)enter image description here

在这里,我只签入了列表,并保存了我关闭此屏幕(数据将在添加到列表文本单击时保存)enter image description here

在这里,即使我从蜂巢中获取新数据,我也在一秒钟内找到了未保存的数据enter image description here

```
import 'dart:convert';

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:mg_planner_app/data/data_default_category.dart';
import 'package:mg_planner_app/data/data_general.dart';
import 'package:mg_planner_app/setting/model/model_country.dart';


class GeneralBox {
  static const String data_general = "data_general";
  static const String data_category = "data_category";
  static const String data_sub_category = "data_sub_category";
  static const String data_item = "data_item";
  static const String data_setting = "data_setting";

  static Box<DataGeneral> GetGeneral() => Hive.box(data_general);

  static Box<DataCategory> GetCategory() => Hive.box(data_category);

  static Box<DataSubCategory> GetExpense() => Hive.box(data_sub_category);

  static Box<DataItem> GetAccounts() => Hive.box(data_item);

  static Box<DataSetting> GetDataSetting() => Hive.box(data_setting);

  static DataSetting GetDataSettingObject() {
    var box_setting = GetDataSetting();
    if(box_setting.values.length > 0) {
      return box_setting.values.toList().cast<DataSetting>()[0];
    }
    else {
      final dataSetting = DataSetting()
        ..key = "0"
        ..user_name = "Guest"
        ..due_term = "7"
        ..number_formate_index = "3"
        ..number_formate_char = ","
        ..date_format = "dd/MM/yyyy"
        ..time_format = "hh:mm a"
        ..uuid = "uuid.v1()"
        ..modelCountry = jsonEncode(ModelCountry.countries[185])
        ..pass_code = false
        ..is_show_board = true
        ..is_purchased = false
        ..currency_format = 4
        ..password = "";

      // print("objectobject 555 ${ModelCountry.countries[185].toJson()}");
      box_setting.put("0", dataSetting);
      return dataSetting;
    }
  }

  static void saveDataSetting(DataSetting dataSetting) {
    var box_setting = GetDataSetting();
    box_setting.put("0", dataSetting);
  }





  static GetHiveFunction() async {
    // Hive.close;
    await Hive.initFlutter();
    if(!Hive.isAdapterRegistered(0)) {
      Hive.registerAdapter(DataGeneralAdapter());
    }
    if(!Hive.isAdapterRegistered(1)) {
      Hive.registerAdapter(DataCategoryAdapter());
    }
    if(!Hive.isAdapterRegistered(2)) {
      Hive.registerAdapter(DataSubCategoryAdapter());
    }
    if(!Hive.isAdapterRegistered(3)) {
      Hive.registerAdapter(DataItemAdapter());
    }
    if(!Hive.isAdapterRegistered(4)) {
      Hive.registerAdapter(DataSettingAdapter());
    }

    //============================================

    if(!Hive.isBoxOpen(data_general)) {
      await Hive.openBox<DataGeneral>(data_general);
    }
    if(!Hive.isBoxOpen(data_category)) {
      await Hive.openBox<DataCategory>(data_category);
    }
    if(!Hive.isBoxOpen(data_sub_category)) {
      await Hive.openBox<DataSubCategory>(data_sub_category);
    }
    if(!Hive.isBoxOpen(data_item)) {
      await Hive.openBox<DataItem>(data_item);
    }
    if(!Hive.isBoxOpen(data_setting)) {
      await Hive.openBox<DataSetting>(data_setting);
    }

    //============================================
  }

  static String getNewKey() {
    return DateTime.now().millisecondsSinceEpoch.toString();
  }



  static List<DataCategory> ListDefaultCategory() {
    List<DataCategory> list = [];

    list.add(dataCategory1);
    list.add(dataCategory2);
    list.add(dataCategory3);
    list.add(dataCategory4);
    list.add(dataCategory5);
    list.add(dataCategory6);
    list.add(dataCategory7);
    list.add(dataCategory8);
    list.add(dataCategory9);
    list.add(dataCategory10);
    list.add(dataCategory11);
    list.add(dataCategory12);
    list.add(dataCategory13);
    return list;
  }


  static DataCategory AddNewCategory(String category_type, String category_name) {

    return DataCategory()
      ..key = getNewKey()
      ..type = category_type
      ..name = category_name
      ..list_item = []
      ..sub_category = [];
  }

  static DataSubCategory AddNewSubCategory(String category_type, String category_name) {

    return DataSubCategory()
      ..key = getNewKey()
      ..type = category_type
      ..name = category_name
      ..list_item = [];
  }




}

然后我使用

ValueListenableBuilder<Box<DataCategory>>(
        valueListenable: GeneralBox.GetCategory().listenable(),
        builder: (context, box, _) {
          List<DataCategory> list_category = 
数据库 Flutter Hive

评论


答: 暂无答案