Flutter databases and Hive

Tamil KannanCV
2 min readJul 16, 2022

--

Hi there, In this blog I am sharing a detailed information about Flutter database and Hive in flutter.

Databases

Databases are a kind of storage medium that are used to store app’s data either in cloud or local storage based on the requirements. Databases are of different types, But they can widely be classified into

  • SQL Databases (Relational databases)
  • NoSQL Databases

The main difference between the above two databases are that in SQL database the data is stored in a relational format i.e Tables (Rows and Columns), but in NoSQL Databases the data is stored in key and values format.

Since NoSQL is faster than SQL, it is been widely used in modern applications. But SQL databases are also popularly used in legacy sofwares that require more relations.

In Flutter we have various plugins for both SQL and NoSQL databases, they are:

  • Moor
  • Floor
  • Hive
  • sqflite
  • Firebase Firestore DB (cloud based)
  • ObjectBox

Among these databases Hive has the most likes from pub.dev since it is NoSQL based and is easy to use and implement.

It stores the data in Keys and values format and it can store primitive data types like List, Map, DateTime, BigInt and Uint8List. It also provides support to save Objects directly into the database.

Dependencies

dependencies:
hive: ^[version]
hive_flutter: ^[version]
dev_dependencies:
hive_generator: ^[version]
build_runner: ^[version]

Add the above dependencies into pubspec.yaml and run pub get command.

Initialising hive:

await Hive.initFlutter();

You can also specify a sub-directory for flutter hive.

Hive boxes

Hive stores data in boxes, Here boxes are similar to tables in SQL dbs.

var box = await Hive.openBox('myBox');

The hive boxes can be referenced anywhere in the app without using asyc/await.

var box = Hive.box('myBox');

Try your codes in DartPad:

I appreciate your patience reading this blog,do follow for more interesting stuffs like this. Happy coding….

--

--