flutter hive的使用

导包:

  hive: ^2.0.6
  hive_flutter: ^1.1.0

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

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: () {},
        ));
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值