Created
April 26, 2020 04:37
-
-
Save Zfinix/c37893f4d19458bc8e4a2bf2acd3d3f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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