Exploring ISAR: A Step-by-Step Guide to Database Management in Flutter

Ashish Sharma
3 min readSep 2, 2023

--

As a Flutter developer, you understand the importance of efficient data management in your applications. Enter ISAR, a high-performance, embedded NoSQL database designed exclusively for Flutter. In this step-by-step guide, we’ll explore what ISAR is, why it’s a fantastic choice for your Flutter projects, and how to implement it with real-world examples.

What is ISAR?

ISAR is a database solution created to simplify data management in Flutter applications. It offers exceptional performance, seamless integration with Flutter’s ecosystem, and real-time data synchronization. Whether you’re building a simple to-do list app or a complex e-commerce platform, ISAR can handle your data efficiently.

Why Choose ISAR for Flutter?

Before we dive into the implementation, let’s understand why ISAR is a compelling choice:

  • Exceptional Performance: ISAR leverages native code, ensuring speedy and efficient database operations, even with large datasets.
  • Flutter-Centric Design: It seamlessly integrates with Flutter, allowing you to use familiar Dart code for database management without platform-specific implementations.
  • Real-Time Sync: ISAR offers real-time data synchronization, ideal for apps requiring instant updates, such as messaging or collaborative tools.
  • Schemaless Flexibility: ISAR supports schemaless data structures, allowing you to adapt your data model on-the-fly without migration complexities.

Now that we’ve got the basics, let’s dive into the implementation.

Implementing ISAR in Your Flutter Project

Step 1: Add ISAR to Your Dependencies

Start by adding ISAR to your Flutter project’s dependencies in the pubspec.yaml file:

dependencies:
isar: latest_version
isar_flutter_libs: latest_version # contains Isar Core

dev_dependencies:
isar_generator: latest_version
build_runner: any

Make sure to replace latest_version with the current ISAR version.

Step 2: Initialize ISAR

In your Flutter app’s entry point (usually main.dart), initialize ISAR, configure it as needed, and open your database. Here's an example:

import 'package:isar/isar.dart';

Future<void> main() async {
final isar = await openIsar();
// Use ISAR for database operations.
}

Build Runner will now automatically generate the necessary code to manage your ISAR database, saving you valuable development time.

Step 3: Define Your Data Models

Create your data models as Dart classes, annotating them with ISAR annotations. These annotations define how your data should be stored in the database. For example:

import 'package:isar/isar.dart';

@Collection()
class Person {
Id id = Isar.autoIncrement; // you can also use id = null to auto increment

late final name = IsarText();

late final age = IsarInt();
}

In this example, we define a Person class with a name and age, specifying their data types as text and integer, respectively.

Step 4: Saving Data

Now that your data model is ready, you can save data to the database as you normally would. Build Runner will handle the code generation for these operations. Here’s an example of how to add a new person:

final newPerson = Person()..name = 'John'..age = 30;
await isar.writeTxn((isar) async {
await isar.persons.put(newPerson);
});

Step 5: Querying Data

Retrieving data remains as straightforward as before. To get all persons older than 25, you can do this:

final persons = await isar.persons
.where()
.ageGreaterThan(25)
.findAll();

Step 6: Updating and Deleting Data

Updating and deleting data is still a breeze. To update a person’s age, for example:

Future<void> updatePerson() async {
await isar.writeTxn((isar) async {
final personToUpdate = await isar.persons.where().nameEqualTo('John').findFirst();
if (personToUpdate != null) {
personToUpdate.age = 31;
await personToUpdate.save();
}
});
}

And to delete a person:

Future<void> deletePerson() async {
await isar.writeTxn((isar) async {
final personToDelete = await isar.persons.where().nameEqualTo('John').findFirst();
if (personToDelete != null) {
await personToDelete.delete();
}
});
}

Conclusion

You’ve successfully implemented ISAR, the powerful database solution for Flutter, enhanced with the efficiency of Build Runner. With ISAR’s exceptional performance and Flutter-centric design, managing your app’s data is more straightforward than ever. Whether you’re building a small project or a complex application, ISAR empowers you to handle data efficiently, providing a smooth user experience.

Now, integrate ISAR into your Flutter projects and enjoy the seamless data management capabilities it brings. With Build Runner’s code generation, your development process becomes more streamlined, allowing you to focus on building outstanding Flutter apps. Happy coding! 🚀✨

--

--

Ashish Sharma

TheFlutterist: Teaching Flutter in the most playful way possible. Join me in the land of widgets and hot reloads! http://flutterist.in/