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: () {},
        ));
  }
}
相关推荐
码字的字节2 小时前
ZooKeeper在Hadoop中的协同应用:从NameNode选主到分布式锁实现
hadoop·分布式·zookeeper·分布式锁
liliangcsdn16 小时前
mac测试ollama llamaindex
数据仓库·人工智能·prompt·llama
码字的字节18 小时前
深入解析YARN中的FairScheduler与CapacityScheduler:资源分配策略的核心区别
hadoop·capacity·fairscheduler
Johny_Zhao18 小时前
Centos8搭建hadoop高可用集群
linux·hadoop·python·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm
晴天彩虹雨1 天前
统一调度与编排:构建自动化数据驱动平台
大数据·运维·数据仓库·自动化·big data·etl
码字的字节2 天前
深入解析Hadoop高频面试题:HDFS读/写流程的RPC调用链
hadoop·hdfs·rpc
白日与明月2 天前
Hive-vscode-snippets
hive·hadoop·vscode
Sirius Wu2 天前
Hive的窗口函数
数据仓库·hive·hadoop
码字的字节2 天前
深入解析HBase如何保证强一致性:WAL日志与MVCC机制
hadoop·hbase·wal·mvcc
シ風箏2 天前
Hive【安装 01】hive-3.1.2版本安装配置(含 mysql-connector-java-5.1.47.jar 网盘资源)
java·hive·mysql