Flutter development series
Part 3 – Using databases in Flutter

flutter-development-series-databases

Welcome to part 3 of the Flutter development series. In this post, we will explore using databases in Flutter. We are going to be adding a data layer to our loyalty app and also explore Hive.

A new episode will be published fortnightly-ish. The currently and eventually final version of the app can be found in Github.

Databases and Flutter

Let’s go through a quick intro on available and commonly used databases in Flutter.

If you have developed for mobile before, or even if you haven’t, you probably came across SQLite at some point. Having said that, it was probably the first thing that came in mind when I said databases.

Well. Before we continue, let’s clarify that we won’t be using a relational database for this project. I will explain why.

Firstly, as for the time I’m writing this post, working with relational databases in Flutter is not as straight forward as native Android. Why do I say that? Well, there are no equivalent to Room or Realm for Flutter, for example. The ones available for Dart/Flutter need a lot of boilerplate code.

On the other hand , If you must use a relational db, look at the following libraries:

Secondly, not all problems are solved using the same tools.

tool
Use the right tool for the right job!

For our simple loyalty cards application, having a relational database would be an overkill. Having said that, let’s have a look at Hive.

What’s Hive?

If you haven’t heard about Hive, it’s a key-value database written in pure Dart.

It’s very simple, easy to use and lightning fast.

Now that we have clarified few things, let’s add Hive to our project and setup the loyalty cards repository.

Adding Hive to the project

Open your project, create a new Flutter project or simply clone the loyalty app Github repo.

If you need, the previous post shows you how to setup your flutter environment.

Finally, add the library to the pubspec.yaml file. We are also going to add some extra dev dependencies. I’ll explain why later on.

And install it:

$ flutter pub get

Creating a hive object for the loyalty card

Let’s create a new Hive object that represents a loyalty card to be shown in our application.

We only need couple of fields to be stored for a given loyalty card. We basically need a number and a description.

Create a new file in the project and name it loyaltycard.dart:

We also need to generate the hive object adapter for the class we just created. Hence the build_runner and hive_generator dependencies added previously. Type the following in the terminal:

$ flutter packages pub run build_runner build

Creating a hive box for the loyalty cards

All data stored in Hive is organised in boxes. A box can be compared to a table in SQL, but it does not have a structure and can contain anything.

http://docs.hivedb.dev/

Create a new file for our loyalty card Hive box.

Well, that was pretty short! I want more!

Alright! Alright! Let’s then expose it through a nice and tidy repository pattern for our loyalty cards.

Combining Hive box with a repository pattern in Flutter

Why should I use the repository pattern? It helps us limiting the dependency on Hive to the data layer. Therefore, if we ever want to replace Hive, we won’t need to refactor the whole application. Hive’s dependency is restricted only in the data layer and exposed thought a repository pattern to the presentation layer. Let’s call it a loosely coupled app architecture.

Now that we have cleared that up. Let’s create an abstract class to help us test and define our repository pattern and implement it.

Unit testing a repository in Flutter

What? You thought I would forget the tests? I actually TDD it!

self-five-himym

Sorry, got off track. Create a new file to test our repository:

Unit testing Hive in Flutter

Given the unit test above, we are still not testing the integration between Hive and our repository. In short, the test is not enough to give us the certainty that our code will work.

Noticed how I said integration above? That’s exactly how we are going to call testing Hive, an integration test. It’s a common mistake for people to call the test bellow a unit test.

Create a new file for our integration test:

Conclusion

If this post helped you on understanding how to use Hive and also test it. My mission has been accomplished. If it didn’t on the other hand, your feedback is much appreciated.

Remember that all the code snippets you see at the Flutter development series posts is part of our Loyalty App and it’s available on Github.

Thank you for your time. You are ready to proceed to the next post:

Flutter development series – Part 4 – BLOC pattern in Flutter


0 Comments

Leave a comment