首页 > 编程问答 >如何在 Hive 中存储类列表?

如何在 Hive 中存储类列表?

时间:2024-06-03 20:53:27浏览次数:14  
标签:flutter hive local-storage

我有一个通过模型类从 api 获取的 itemlist,现在我想将该列表保存到 hive 中,并添加一些其他属性,如客户名称、客户地址和 itemsList。

import 'package:hive/hive.dart';
导入'.../item_model.dart';
导入'hive_model.dart';
part 'saved_hive_model.g.dart';
@HiveType(typeId: 15)
类 SavedDatInHive {
@HiveField(10)
final String totalPrice;
@HiveField(11)
final List<ItemList> list;
@HiveField(12)
final String CustomerName;
SavedDatInHive(this.list, this.CustomerName, {required this.totalPrice});}
</pre


import 'package:hive/hive.dart';
import 'item_model.dart'; 

part 'saved_hive_model.g.dart';

@HiveType(typeId: 15)
class SavedDataInHive { // 注意:类名已更正为 SavedDataInHive
  @HiveField(10)
  final String totalPrice;

  @HiveField(11)
  final List<ItemModel> list; // 使用 ItemModel 类型

  @HiveField(12)
  final String customerName; // 注意:变量名已更正为 customerName

  @HiveField(13)
  final String customerAddress; // 新增 customerAddress 属性

  SavedDataInHive({
    required this.totalPrice,
    required this.list,
    required this.customerName,
    required this.customerAddress,
  });
}

// 保存数据到 Hive
Future<void> saveDataToHive(List<ItemModel> items, String customerName, String customerAddress, String totalPrice) async {
  // 初始化 Hive
  await Hive.initFlutter();
  // 注册 adapter
  Hive.registerAdapter(SavedDataInHiveAdapter());
  Hive.registerAdapter(ItemModelAdapter()); // 注册 ItemModel 的 adapter

  // 打开 Hive box
  var box = await Hive.openBox('myBox'); 

  // 创建 SavedDataInHive 对象
  var savedData = SavedDataInHive(
    totalPrice: totalPrice,
    list: items, 
    customerName: customerName,
    customerAddress: customerAddress,
  );

  // 将数据添加到 Hive
  await box.add(savedData); 
}

// 从 Hive 读取数据
Future<List<SavedDataInHive>> loadDataFromHive() async {
  // 初始化 Hive
  await Hive.initFlutter();
  // 注册 adapter
  Hive.registerAdapter(SavedDataInHiveAdapter());
  Hive.registerAdapter(ItemModelAdapter());

  // 打开 Hive box
  var box = await Hive.openBox('myBox');

  // 获取所有数据
  List<SavedDataInHive> savedDataList = [];
  for (int i = 0; i < box.length; i++) {
    savedDataList.add(box.getAt(i));
  }

  return savedDataList;
}

解释:

  1. 数据模型 :
  2. SavedDataInHive 类名修改为更准确的名称。
  3. list 的类型改为 List<ItemModel> ,以正确存储 ItemModel 对象列表。
  4. CustomerName 更正为 customerName ,以遵循驼峰命名法。
  5. 添加了 customerAddress 属性来存储客户地址。

  6. 保存数据 :

  7. saveDataToHive 函数现在接受 items , customerName , customerAddress totalPrice 作为参数。
  8. 使用 Hive.registerAdapter 注册 ItemModelAdapter ,确保 Hive 可以正确存储和读取 ItemModel 对象。
  9. 创建一个 SavedDataInHive 对象,并将所有数据传递给其构造函数。
  10. 使用 box.add(savedData) 将数据添加到 Hive。

  11. 读取数据 :

  12. loadDataFromHive 函数从 Hive 中读取所有 SavedDataInHive 对象,并将其作为列表返回。

使用示例:

// 假设你已经从 API 获取了 itemlist,customerName,customerAddress 和 totalPrice

// 保存数据
await saveDataToHive(itemlist, customerName, customerAddress, totalPrice);

// 读取数据
List<SavedDataInHive> savedDataList = await loadDataFromHive();

// 打印第一个保存数据的客户姓名
print(savedDataList[0].customerName); 

请注意:

  • 你需要为 ItemModel 类创建 HiveObject TypeAdapter 。可以参考 Hive 文档中的说明进行操作:https://docs.hivedb.dev/#/basics/adapters
  • 你需要在 pubspec.yaml 文件中添加 hive hive_generator 依赖项,并运行 flutter packages get 命令。

标签:flutter,hive,local-storage
From: 78569753

相关文章

  • DataX HiveReader
    DataXHiveReader来源:github-datax-hivereader1快速介绍Hivereader插件:从Hive表读取数据2实现原理实现方式是:根据配置的QuerySql,通过将查询结果保存到一张新的临时hive表中这种方式;然后获取临时表的hdfs文件地址,然后读取文件到缓冲区,最后删除临时的表。3功能说明Hiv......
  • Oracle系列---【ORA-00257: Archiver error. Connect AS SYSDBA only until resolved
    ORA-00257:Archivererror.ConnectASSYSDBAonlyuntilresolved1.问题描述过了个周末,发现系统登录不上了,查看日志,发现报"ORA-00257:Archivererror.ConnectASSYSDBAonlyuntilresolved",明显数据库有问题了,我用客户端连,也报这个错误。2.问题分析经过一番查询,是因......
  • Flutter开发效率提升1000%,Flutter Quick教程之对写好的Widget进行嵌套
    通常写代码的时候,我们是先写好外面的Widget,再写里面的Widget。但是,也有的时候,我们写好了一个Widget,但是我们觉得有必要再在外面嵌套一个Widget,这时候应该怎么做呢?(还有其他方式,本篇讲的就是快捷的方式)1,首先,我们左键选中要嵌套的Widget。2,这时候,我们要选择的嵌套在外面的Widge......
  • 【Flutter】路由详解
    ......
  • 【Flutter】Getx上篇
    ......
  • 【Flutter】Getx下篇
    ......
  • flutter 空安全、late延迟及required关键词
    空安全Dart和Kotlin一样都是支持空安全,空安全操作符主要有两个:?可空类型!类型断言可空类型在之前我们的介绍中,声明一个变量,如:Stringstr="A";str=null;这个时候str=null代表会报错,提示Avalueoftype'Null'can'tbeassignedtoavariableoftype'String......
  • flutter 监听网络HTTP数据流处理
    flutter网络用于HTTP的交互中,有Http和Dio两种方式,本次侧重介绍dio的简单使用1.flutter安装dio插件https://pub-web.flutter-io.cn/中搜索dio插件使用,详细安装如下:2.使用(1)创建CancelToken对象,可然后采用异步的方式进行数据post(本次交互使用post数据的方式),核心......
  • 在我的 Flutter 应用程序中尝试通过 sso 登录时,我看到了一个黑屏
    我正在调用android上flutter_appAuth软件包的authorize方法。下面是该方法的外观:finalresult=awaitFlutterAppAuth(.authorize())finalresult=awaitFlutterAppAuth().authorize(授权请求SSOConstants.clientId、SSOConstants.redirec......
  • Flutter 中的 Opacity 小部件:全面指南
    Flutter中的Opacity小部件:全面指南在Flutter中,动画和视觉效果是提升用户体验的重要手段。Opacity小部件允许你改变子组件的透明度,从而实现淡入、淡出或其它透明度相关的动画效果。本文将提供Opacity的全面指南,帮助你了解如何使用这个小部件来增强你的Flutter应用的视觉......