Which one should you use Shared Preferences or Hive?

Tamil KannanCV
2 min readJun 23, 2023

--

Both Hive and Shared Preferences are meant to store app data for later usage, but there are huge differences between them.

What is Shared Preferences?

Shared Preferences helps in storing app data in the key-value pair format, which helps easy retrieval and usage of data inside the app.

Shared Preferences is a dart own way of storing app data in the local device. It is restricted towards primitive data types like String, Integer, Boolean etc.,

What is Hive?

Hive is kind of no-SQL database, which is lightweight and faster with many features. It is straightforward method to key-value pair approach. Similar to Shared Preferences it also stores the data in the local device.

Let’s consider a scenario where we have to store the data of username of the app to local storage.

When using Shared Preferences, the code be like,

The same can be implemented in Hive as follows,

You can see the differences in the above snippets, but both of them seems to be the same in terms of code size and complexity, but actually it is not. In hive we can also store the data in key-value pairs, here we create boxes which are used to store a variety of data based on the needed categories.

You can also save your own model in the Hive database with no more code for its conversion. To do so we need to implement Adapters which takes care the conversion process itself.

To learn more about Hive and Hive adapters,

These two packages have their unique pros and cons with them, The purpose of storing says which one to choose, I’ll leave the opition to you. My personal opinion is using Hive, which would make our app faster and more reliable.

--

--