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: () {},
        ));
  }
}
相关推荐
修先生8 小时前
Hive Udf函数AES加密
hive·aes·udf
SeaTunnel13 小时前
Apache SeaTunnel 4 月有何新动作?连接器增强与 Zeta 稳定性提升等亮点速览
大数据·数据仓库·spark·apache·seatunnel
Volunteer Technology13 小时前
HDFS源码(二)
大数据·hadoop·hdfs
承渊政道14 小时前
Oracle迁移避坑:一个(+)写错,LEFT JOIN可能变INNER JOIN
运维·服务器·数据库·数据仓库·学习·安全·oracle
WL_Aurora14 小时前
MapReduce框架原理深度解析 | Shuffle机制、切片分区、Join全攻略
大数据·hadoop·mapreduce
赵渝强老师14 小时前
【赵渝强老师】Hadoop的伪分布部署模式
大数据·hadoop·分布式
Volunteer Technology14 小时前
HDFS源码(一)
大数据·hadoop·hdfs
AllData公司负责人1 天前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
WL_Aurora1 天前
HDFS底层原理深度解析 | 读写流程、NameNode工作机制、DataNode心跳与数据完整性
大数据·hadoop·hdfs
Pushkin.1 天前
新数仓建设方法论与实践指南-分层解耦驱动的数据仓库
大数据·数据仓库