Monday, June 17, 2024
Google search engine
HomeObjectBox: A NoSql database for Flutter/Dart

ObjectBox: A NoSql database for Flutter/Dart

There are few Databases in Flutter that are fast, efficient, and easy to use but when ObjectBox made its debut in Flutter, not only list grown longer but some key points have also been sorted out like improving performance and bringing much-awaited relations support from language bindings to flutter.

For those of you new to ObjectBox: ObjectBox is a superfast NoSQL object database for Flutter / Dart and here is how you can save data in your Dart / Flutter apps

In this blog, we will learn about ObjectBox: A NoSql database for Flutter/Dart. We will also implement an ObjectBox: A NoSql database for Flutter/Dart, describe his properties. So let’s get started. so let’s get started.


Table Of Contents :

ObjectBox: A NoSql database.

ObjectBox Key Features.

How to fast ObjectBox Dart

Implement

Code Implementation

Conclusion


ObjectBox: NoSql Database:

Objectbox in Flutter is a very fast NoSQL database and it is optimized for very high performance on devices like mobile desktop and very lightweight database it stores all the objects.

ObjectBox Key Features:

There are some Key Features of ObjectBox are:

  • LIGHTSPEED: OjectBox carrying the motto of best performance and is up to the mark when it comes to delivering it, till now it has outperformed on every embedded database tested so far.
  • EASY OBJECT API: ObjectBox is specially designed for Mobile Applications and IoT Devices, it requires only a little bit of code as compared to SQLite with consisting of any kind of rows, columns, or SQL.
  • QUERYBUILDER: With ObjectBox, simply query for objects with checks at compile times; no more typos causing crashes at runtime.
  • OBJECT RELATIONS: In ObjectBox references and the relationship of Object are built-in, native references.
  • REACTIVE: Reacting to data changes is simple and powerful. Use reactive data observers from ObjectBox or integrate with RxJava.
  • MULTIPLATFORM: ObjectBox supports C, Android, plain-Java, Kotlin (Linux and Windows), POSIX, MacOS. Swift (iOS) is coming up soon.
  • INSTANT UNIT TESTING: Unit testing is with a real database is easy in ObjectBox and for this, it takes a few milliseconds, it is because of its multi-platform approach.
  • ROBUST TECHNOLOGY: Safe transactions and parallelism has been provided to you because of Its Multiversion Concurrency Control(MVCC) and ACID (Atomic, Consistent, Isolated, Durable) properties.
  • SIMPLE THREADING: Objects returned by ObjectBox within all threads. No strings attached.
  • SCHEMA MIGRATIONS: ObjectBox takes care of new object versions with added, removed, and renamed properties.
  • SYNC: ObjectBox Synchronizes online/offline data seamlessly with simple APIs.

How fast is ObjectBox Dart?

Speed is important for data persistence solutions. Accordingly, we wanted to test how ObjectBox compares performance-wise to other Flutter Dart database options. Therefore, we looked for libraries with comparable levels of storage abstraction and feature set — so not just plain SQL/Key-value storage but also ORM-like features. There doesn’t seem to be that much choice…

We looked at two popular approaches: sqflite an SQLite wrapper for Flutter (no Dart Native support), and Hive, a key-value store with Class-adapters which seems still popular although its technology is phased out (see below). As a third alternative, we pulled in Firestore, which does not really fit as it is no local database, but would be fun to compare anyway.

ObjectBox Data Sync:

ObjectBox is a kind of out-of-box data sync way to keep our data in-sync online and offline. This outbox keeps the data up to date between devices like mobile, IoT, and cloud servers, and this ObjectBox when the client comes back online Automatically updates this change.

Implementation :

You need to implement it in your code respectively :

Step 1: Add dependencies.

Add dependencies to pubspec — yaml file.

dependencies:

objectbox: ^0.11.0
objectbox_flutter_libs: any
path_provider: ^1.6.27

dev_dependencies:

build_runner: ^1.0.0
objectbox_generator: any

Step 2: import the package :

import 'package:objectbox/objectbox.dart';
import 'package:path_provider/path_provider.dart';

Step 3: Run flutter packages get in the root directory of your app.

Code Implementation :

Firstly to implement the database we will create a model class that will have @Entity() annotation and use it later to get the bindings required by ObjectBox.

Here is how lib/model/person_model.dart looks in my case:

import 'package:objectbox/objectbox.dart';

@Entity()
class PersonDetail {
String name;
String lastName;
int personAge;
int id;

PersonDetail({this.name, this.lastName, this.personAge});
}

You can notice that I have left the id parameter out of the class constructor. The reason for this is that objectbox will dynamically allocate a new id for the model class and we don’t need to worry about manually incrementing it after every operation.

Once you have implemented your model class, run the build_runner so that the objectbox can generate the required binding files.

After this, we have created a helper class in which we have defined the Person Details in which we can insert the name of the person and all his details and with the help of his id we can delete his details and update it as That we have given below reference of the code like delete insert and update.

class Helpers {

static Future<int> insert(PersonDetail person) async {
var store = await ObjectBoxStore.getStore();
var box = store.box<PersonDetail>();
return box.put(person);
}

static Future<bool> delete(int id) async {
var store = await ObjectBoxStore.getStore();
var box = store.box<PersonDetail>();
return box.remove(id);
}

static Future<PersonDetail> queryPerson(int id) async {
var store = await ObjectBoxStore.getStore();
var box = store.box<PersonDetail>();
return box.get(id);
}
}

Code File :

import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_objectbox_demo/helpers/helpers.dart';
import 'package:flutter_objectbox_demo/model/person_model.dart';
class ObjectBoxDemo extends StatefulWidget {
@override
_ObjectBoxDemoState createState() => _ObjectBoxDemoState();
}

class _ObjectBoxDemoState extends State<ObjectBoxDemo> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title:Text('Object Box Demo'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: Text('INSERT'),
onPressed: () async {
var person =
PersonDetail(name: "Mac", lastName: "anthony", personAge: 25);
int id = await Helpers.insert(person);
print("Sucessfull inserted an object with $id");
},
),
SizedBox(height:20,),
RaisedButton(
child: Text('UPDATE'),
onPressed: () async {
var person = await Helpers.queryPerson(1);
person.name = 'Sam';
person.lastName = 'david';
person.personAge = 30;
int id = await Helpers.insert(person);
print("Sucessfull updated an object with $id");
},
),

SizedBox(height:20,),
RaisedButton(
child: Text('DELETE'),
onPressed: () async {
bool deleted = await Helpers.delete(1);
if (deleted) {
print("Sucessfull deleted an object with id :1");
}
},
),
],
),
);
}
}

Conclusion :

In this article, I have explained ObjectBox: A NoSql database for Flutter/Dart?, which you can modify and experiment with according to your own, this little introduction was from the ObjectBox: A NoSql database for Flutter/Dart?.

I hope this blog will provide you with sufficient information on Trying up the ObjectBox: A NoSql database for Flutter/Dart? in your flutter project. We ObjectBox: A NoSql database for Flutter/Dart? is and work on it in your flutter applications, So please try it.

❤ ❤ Thanks for reading this article ❤❤

If I got something wrong? Let me know in the comments. I would love to improve.

Clap 👏 If this article helps you.


Feel free to connect with us
And read more articles from FlutterDevs.com.

FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter-related queries.

We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments