Skip to content

Instantly share code, notes, and snippets.

@Zfinix
Created April 26, 2020 04:37
Show Gist options
  • Select an option

  • Save Zfinix/c37893f4d19458bc8e4a2bf2acd3d3f9 to your computer and use it in GitHub Desktop.

Select an option

Save Zfinix/c37893f4d19458bc8e4a2bf2acd3d3f9 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
void main() async {
Hive..init(Directory.current.path);
await HiveDB.openBox('my_db');
HiveDB.saveItem(item: 'Chizi', key: 'Name');
print(await HiveDB.getItem('Name')); //returns Chizi
}
abstract class HiveDB {
static var boxName = '';
static Future<Box<dynamic>> openBox(_) async {
boxName = _;
return await Hive.openBox(boxName);
}
static saveItem({@required item, @required key}) async {
var box = Hive.box(boxName);
box.put(key, item);
}
static eraseItem({@required key}) async {
var box = Hive.box(boxName);
box.delete(key);
}
static eraseItems({List<String> keys}) async {
var box = await Hive.openBox(boxName);
box.deleteAll(keys);
}
static eraseAll() async {
await Hive.deleteFromDisk();
}
static getItem(key) async {
var box = Hive.box(boxName);
return box.get(key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment