Add the following to your pubspec.yaml
:
dependencies:
hive: ^[version]
hive_flutter: ^[version]
dev_dependencies:
hive_generator: ^[version]
build_runner: ^[version]
Initializes Hive with a valid directory in your app files. You can also provide a subdirectory:
await Hive.initFlutter();
?> Use Hive.init()
for non-Flutter apps.
All of your data is stored in boxes.
var box = await Hive.openBox('testBox');
?> You may call box('testBox')
to get the singleton instance of an already opened box.
Hive supports all primitive types, List
, Map
, DateTime
, BigInt
and Uint8List
. Any object can be stored using TypeAdapters.
import 'package:hive/hive.dart';
void main() async {
//Hive.init('somePath') -> not needed in browser
var box = await Hive.openBox('testBox');
box.put('name', 'David');
print('Name: ${box.get('name')}');
}
Learn the basics of using Hive in this well-made tutorial by Reso Coder.
<iframe id="ytplayer" type="text/html" data-src="https://www.youtube.com/embed/R1GSrrItqUs" class="video"/>