Isar Flutter is an advanced database solution designed specifically for Flutter applications. When managing data efficiently and seamlessly in a Flutter app, Isar Flutter offers a wide range of features that make it a top choice for developers.
In this blog post, we will dive deep into Isar Flutter, exploring its key features and benefits and how to integrate it into your Flutter projects.
Isar Flutter is a fully asynchronous NoSQL database optimized for Flutter. It has been carefully crafted to cater to the unique needs of Flutter developers. With Isar Flutter, you can expect a feature-rich experience that helps you deliver the best possible performance and user experience for your Flutter applications.
One of the standout features of Isar Flutter is its full-text search capability, which allows you to perform lightning-fast searches within your data. Whether you need to search for specific keywords or perform complex queries, Isar Flutter has got you covered.
To start with Isar Flutter, you must add the necessary dependencies to your Flutter project. Update your pubspec.yaml file as follows:
1isar_version: &isar_version 3.1.0 # define the version to be used 2 3dependencies: 4 isar: *isar_version 5 isar_flutter_libs: *isar_version # contains Isar Core 6 7dev_dependencies: 8 isar_generator: *isar_version 9 build_runner: any
Once you have added the dependencies, annotate your collections with @collection and configure their schema. Let's take an example of an Email collection:
1part 'email.g.dart'; 2 3@collection 4class Email { 5 Id id = Isar.autoIncrement; // you can also use id = null to auto increment 6 7 @Index(type: IndexType.value) 8 String? title; 9 10 List<Recipient>? recipients; 11 12 @enumerated 13 Status status = Status.pending; 14} 15 16@embedded 17class Recipient { 18 String? name; 19 20 String? address; 21} 22 23enum Status { 24 draft, 25 pending, 26 sent, 27}
In the above code snippet, we define an Email collection and use annotations to specify its schema. We also define an embedded class Recipient to represent recipients of an email.
Isar Flutter offers many features designed to simplify data management in Flutter applications. Let's explore some of its key features:
Isar Flutter allows you to create composite indexes, which can significantly improve query performance. Combining multiple fields into a single index can speed up the execution of complex queries. Additionally, Isar Flutter supports multi-entry indexes, enabling faster data retrieval based on multiple entries within a single index.
Isar Flutter comes with built-in JSON support, making handling complex data structures within your database more accessible. You can store, query, and manipulate JSON data seamlessly, simplifying your data operations.
You need to open a database instance in your Flutter application to start using Isar Flutter. Here's an example of how you can open a database instance:
1final dir = await getApplicationDocumentsDirectory(); 2final isar = await Isar.open( 3 [EmailSchema], 4 directory: dir.path, 5);
In the code snippet above, we use getApplicationDocumentsDirectory() to get the directory where the database file will be stored. Then, we pass the schema of our Email collection (EmailSchema in this case) to the Isar.open() function along with the directory path.
Isar Flutter simplifies your app's CRUD (Create, Read, Update, Delete) operations. Let's explore how you can perform basic CRUD operations using Isar Flutter:
You can use the put() method within a write transaction to create a new record in an Isar collection. Here's an example:
1await isar.writeTxn(() { 2 await isar.emails.put(newEmail); 3});
In the code snippet above, we create a new email newEmail using the put() method within a write transaction.
You can use the get() method to retrieve records from an Isar collection. Here's an example:
1final existingEmail = await isar.emails.get(newEmail.id!);
In the code snippet above, we retrieve an email record from the emails collection using its ID.
You can use the put() method within a write transaction to update an existing record in an Isar collection. Here's an example:
1await isar.writeTxn(() { 2 await isar.emails.put(updatedEmail); 3});
In the code snippet above, we update an existing email record (updatedEmail) in the emails collection using the put() method within a write transaction.
You can use the delete() method within a write transaction to delete a record from an Isar collection. Here's an example:
1await isar.writeTxn(() { 2 await isar.emails.delete(existingEmail.id!); 3});
In the code snippet above, we delete an existing email record (existingEmail) from the emails collection using the delete() method within a write transaction.
Isar Flutter provides a powerful query language that allows you to perform complex queries, filter data, and sort the results. Here's an example of querying the email collection:
1final importantEmails = isar.emails 2 .where() 3 .titleStartsWith('Important') 4 .limit(10) 5 .findAll(); 6 7final specificEmails = isar.emails 8 .filter() 9 .recipient((q) => q.nameEqualTo('David')) 10 .or() 11 .titleMatches('*university*', caseSensitive: false) 12 .findAll();
In the code snippet above, we perform two different queries. The first query retrieves emails with titles starting with "Important" and limits the result to 10 records. The second query filters emails by recipient name equal to "David" or title containing the word "university" (case insensitive).
Isar Flutter has a database inspector allows you to inspect Isar instances and collections in real time. Using the inspector, you can execute queries, edit properties, switch between instances, and sort data. To launch the inspector, run your Isar app in debug mode and open the inspector link in the logs.
Isar Flutter is a powerful database solution designed explicitly for Flutter applications. With its range of features, including full-text search, composite indexes, and JSON support, Isar Flutter simplifies data management and enhances the performance of your Flutter apps. Whether you are a beginner or an experienced Flutter developer, Isar Flutter can be the go-to choice for all your data storage needs. Try it and see how it elevates your Flutter app development experience!
Isar Flutter is truly an innovative and reliable database solution for Flutter applications. Its ease of use, comprehensive features, and excellent performance make it a must-have for every Flutter developer.
Mastering Isar Flutter opens up dynamic app possibilities, but building significant app experiences requires more.
Enter DhiWise Flutter Builder!
Design stunning UIs visually, generate production-ready code, integrate data effortlessly, deploy with a click, and collaborate seamlessly. With DhiWise, you can create dynamic, data-driven Flutter apps that are unique by going beyond parameters.
Dive in and experience the difference!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.