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: () {},
        ));
  }
}
相关推荐
starzy199010 小时前
Structured Streaming 模块与编程模型:引擎内部到底由什么组成
大数据·hadoop·hdfs
AI源码2 天前
基于 Hadoop 和 Hive 的旅游景区数据的分析与可视化系统
hive·hadoop·旅游
DataY轻量数据平台3 天前
任务调度的三种模式:什么时候跑、按什么顺序跑、等谁跑完
数据仓库·etl
演艺大赏3 天前
三个坑连环套:一次 NameNode 起不来,从 JournalNode 未格式化查到配置里多了个挂载点
大数据·hadoop
宽海智能仓储物流3 天前
宽海智能WCS系统常见问题全解析:从选型对接、运维定制到降本增效
运维·数据仓库·自动化
starzy19903 天前
Hadoop介绍总结:一张图理清组件、生态与版本
java·hadoop
演艺大赏5 天前
两个 NameNode 一起起不来以及Hadoop 3.3.2 里那个认不出自家 JournalNode 的 Servlet
大数据·hadoop
演艺大赏5 天前
RESTServer 起不来,日志只有一行:headerCacheSize 65536 exceeds maximum value of 65534
大数据·hadoop·hbase
吾AI科技7 天前
基于Tez引擎的 Hive SQL 性能优化 —— 为什么脚本运行越来越慢
大数据·hive·性能优化·tez
上海潭龙东海科技有限公司7 天前
客运站管控之动态孪生技术方案(新增一屏管控模块
数据仓库·人工智能·算法·重构·动态规划