Flutter ObjectBox — The Complete Database Crash Course

Flutter ObjectBox — The Complete Database Crash Course

ObjectBox is a quite popular NoSQL database with many features. It has a high performance and has out-of-the-box synchronization as well as offline first support, so users have an “always-on” feeling without a constant internet connection. There are many more advantages, for example, ObjectBox reduces costs and supports literally every environment.

ObjectBox supports numerous frameworks, and one of them is Flutter. That’s why you will learn how to use ObjectBox in Flutter in this article. Great, let’s get started!

Happy reading!

What you will learn

  1. Create your own ObjectBox model
  2. Open an ObjectBox instance
  3. Interacting with boxes in your database

Okay, now that we know what we will learn, let’s get right into it!

Note: You can find the whole source code provided in this tutorial here

Installation

The first thing we have to do after creating our app is to add objectbox. To do so, we will use the command flutter pub add objectbox.

If you use Flutter (you can also use ObjectBox with Dart only), you also have to add objectbox_flutter_libs. The last thing you have to do is to add build_runner and objectbox_generator as dev dependencies.

Great! But there are some things you need to watch out for.

For XCode/iOS only: increase the deployment target to iOS 11 and, under Architectures, replace *${ARCHS_STANDARD}* with *arm64* (or *$ARCHS_STANDARD_64_BIT*).

For Flutter Desktop apps on Linux: the Flutter snap ships with an outdated version of CMake. Install Flutter manually instead to use the version of CMake installed on your system.

If you’re creating a sandboxed macOS app*: you need to specify an application group. Check all `macos/Runner/.entitlements*files if they contain a**section with correct group ID info. Change the string value to the*DEVELOPMENT_TEAM` you can find in your Xcode settings, plus an application-specific suffix, for example:

macos/Runner/\.entitlements*

<dict>  
    <key>com.apple.security.application-groups</key>  
    <array>  
        <string>FGDTDLOBXDJ.demo</string>  
    <array>  
    ...  
</dict>

Next, in your app code, pass the same string when opening the Store, for example: *openStore(macosApplicationGroup: 'FGDTDLOBXDJ.demo')*. Note: Pick a short group identifier; there's an internal limit in macOS that requires the complete string to be 19 characters or fewer.

To run unit tests on your machine*, download a native ObjectBox library for your machine by running this script in a bash shell (e.g. Git Bash on Windows):*

*bash <(curl -s <https://raw.githubusercontent.com/objectbox/objectbox-dart/main/install.sh>)*

Source

Okay, okay, now let’s finally begin working with ObjectBox.

Create Entity Class

As already said, ObjectBox is a NoSQL database. It works with models, that are called Entities. You define a model with the Entity() annotation. Now you can define your different properties. To specify an id, use the @Id() annotation. You can also set properties that are not supposed to be stored in the database with Transient.

ObjectBox allows you to store ints, strings, doubles, etc., but what about special types like a date? Specify your property as usual with DateTime, but annotate it with @Property(type: PropertyType.date). This stores it as an int in milliseconds but converts it back automatically. That’s nice, right?

This could now look like this:

Now, execute flutter pub run build_runner build to generate important code. Unlike other packages, ObjectBox generates a single file lib/objectbox-model.json and lib/objectbox.g.dart for all classes that are annotated with @Entity() in your project.

Create a Store

A Store is the entry point for ObjectBox. This is your interface to the database and box management. It’s recommended to only have a single Store (database) and keep it open while your app is running.

To create a store, use this code provided by the official documentation:

Now you have to call the create method in your main method before the app starts. The documentation can help here too:

Great! Now we can finally come to the basic box operations.

Box operations

First, you have to open your box:

Now you can use many operations.

  • put to insert one element inside the database (the value given has to be of object User in our example)
  • putMany - Insert a list of users
  • get - Get one specific user by its ID
  • getMany - Get multiple users by giving a list of IDs
  • getAll - Get all the users
  • remove - Remove one user by its ID, return a bool to ensure that it was deleted
  • removeMany - Remove many users by giving a list of IDs
  • removeAll - Delete all the objects in a box.
  • count - Counts the number of objects in a box

Let’s take a look at an example:

There is so much more to learn!

For example, querying and asynchronous operations to just name a few. That’s why I will create multiple parts about ObjectBox. If you don’t want to miss them, I recommend following me!

Further reading & Conclusion

In this article, you have learned the basics of the database solution “ObjectBox”. You have seen how easy it is to use and how powerful it is.

You can unfold the whole power of ObjectBox if you use packages like Freezed, Isar, or Flutter Hooks. If you want to learn these additions, I have entire tutorials about them. Check them out here.

In the following few articles, I will introduce more somewhat complicated packages and explain them. If you don’t want to miss this, I recommend you follow me.

I tried my best to write the most straightforward tutorial which everyone understands. If you appreciate this work, I would be very grateful if you could support this quality content and give me some claps!

Thanks for reading, have a nice day!

NOTICE: This article is based on the documentation of the ObjectBox package. All the information provided here is from this site, and a big part of the source code is taken from the documentation. ObjectBox Documentation Source: https://docs.objectbox.io/

Did you find this article valuable?

Support Tomic Riedel by becoming a sponsor. Any amount is appreciated!