flutter hive的使用

导包:

Dart 复制代码
  hive: ^2.0.6
  hive_flutter: ^1.1.0

代码:(敲一遍,你会懂的)

Dart 复制代码
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';

Future<void> main() async {
  await Hive.initFlutter();
  runApp(const MaterialApp(
    home: H(),
  ));
}

class H extends StatefulWidget {
  const H({super.key});

  @override
  State<H> createState() => _HState();
}

class _HState extends State<H> {
  List<int> records = [];
  int index = 0;

  @override
  void initState() {
    super.initState();
    getData();
  }

  Future<void> getData() async {
    var box = await Hive.openBox('demo');
    var value = box.get("record");
    if (value == null) {
      print('啥也没拿到到');
    } else {
      print('拿到数据了');
      records = value.cast<int>();
      print(records);
    }
  }

  Future<void> addData() async {
    var box = await Hive.openBox('demo');
    box.put("record", records);
    print('添加数据成功');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("hive demo"),
        ),
        body: const Placeholder(),
        floatingActionButton: FloatingActionButton(
          child: const Text("add"),
          onPressed: () {},
        ));
  }
}
相关推荐
迈巴赫车主12 小时前
湖仓一体(Data Lakehouse)简介
大数据·数据仓库·数据湖·湖仓一体
liuxiaowei316 小时前
Winform+WPF双框架实战:喷涂工艺SCADA上位机从0到1搭建(附采集监控源码+车间踩坑实录)
大数据·hadoop·wpf
humbinal2 天前
同时支持 gui & cli 的 parquet 文件查看工具,高性能小清新!
hive·python·rust·spark·开源·github·parquet
RestCloud3 天前
ETL是什么?全域数据集成平台核心能力解析
数据仓库·etl·数据清洗·数据处理·etlcloud·数据集成工具
Gent_倪3 天前
万字详解:数据库、数据仓库、数据湖、湖仓一体
数据库·数据仓库·spark
Microsoft Word4 天前
把RAG从 “能跑” 做到上线
数据仓库·人工智能
hkNaruto4 天前
【大数据】《传统Hadoop大数据技术入门:补充与进阶——从数据格式到智能决策的问答实录》
大数据·hadoop·分布式
吾AI科技4 天前
基于Tez引擎的 Hive SQL 性能优化
大数据·hive·性能优化·tez
Database_Cool_4 天前
数据仓库弹性扩缩容怎么实现?AnalyticDB MySQL 在线扩容 0 中断实战
数据库·数据仓库·mysql·阿里云
牛奶咖啡134 天前
大数据Hadoop运维应用实践——双NameNode高可用Hadoop集群架构(下)
大数据·hadoop·hadoop集群配置文件解析·hadoop高可用集群安装部署·配置指定多台服务器相互免密·配置hadoop服务开机自启·ansible部署hadoop