In app design, it is necessary to permanently save specific data. At that time, a local DB created on the user’s smartphone is designed.
There are various packages for local DB, but this time, I will introduce a fast local DB package called “Isar Database“.
What is Isar Database?
Isar Database is a fast cross-platform database for Flutter. It is a local DB used when there is data you want to permanently save in your app.
There are other local DBs, but Isar Database is popular for the following reasons:
- Speed and Efficiency
- Ease of Use for Beginners
- Being a NoSQL database, it has high scalability
(can be expanded as the app grows)
When is it necessary?
Variables and states (State) used in a normal app will disappear when the app is closed.
However, when designing an app, there are situations where you want to carry over the state and variables that occurred due to user operations to the next startup and process them.
In such cases, by storing data in Isar Database, you can manage data that is permanently saved.
How to Use Isar Database
Isar Database is a document-type NoSQL. In the case of RDB, the saved data is called a record, but in the case of document-type NoSQL, the saved data is called a “collection”.
Here is how to use it.
Preparing the Environment
Add the necessary packages to “pubspec.yaml”.
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
isar: ^3.1.0
isar_flutter_libs: ^3.1.0
dev_dependencies:
build_runner: ^2.3.3
dart_code_metrics: ^5.4.0
flutter_lints: ^2.0.0
flutter_test:
sdk: flutter
isar_generator: ^3.1.0
- isar: Core library of Isar database
- isar_flutter_libs: Additional library to use Isar in Flutter
- build_runner: Tool for generating Dart code
- isar_generator: Isar’s code auto-generation tool. Automatically generates necessary code from the model.
Defining the Collection
Define what kind of data to manage. As an example, define the user information used for login in “user.dart”.
import 'package:isar/isar.dart';
import 'user.g.dart';
@collection
class User {
// ID is automatically incremented by Isar
Id? id = Isar.autoIncrement;
String? userName;
String? eMail;
}
Rule 1: @collection
Write “@collection” at the beginning of the definition. It is an annotation for Isar to recognize it as a database table. It is a marker that says “a collection is defined here”.
Rule 2: Unique ID
When defining a collection, it is necessary to set an ID that can be uniquely recognized. This time we are using “Isar.autoIncrement” for the id. If you use Isar.autoIncrement, Isar will automatically assign a unique ID.
The “user.g.dart” being imported does not exist at this point. By executing the following code in the terminal, “user.g.dart” will be created.
flutter pub run build_runner build --delete-conflicting-outputs
Registering the Collection
Create an object for the data you want to insert and put it to insert the collection.
final user = User()
..userName = 'John Doe'
..eMail = 'john@example.com';
await isar.users.put(user);
About users
What is the “users” in isar.users.put that suddenly appeared?
The users method is used when performing some operation on the user collection. The users method for the user collection is automatically defined by the auto-generated “user.g.dart”.
The name of this collection changes according to the name of the collection defined by the user. For example, if the defined collection is Item, it will be Items.
If you don’t know the method name, refer to “xxx.g.dart”.
Updating the Collection
Updating the collection uses the same put as when registering. Create an object with an ID and put it to update. If the ID already exists, the collection will be updated, and if it does not exist, it will be inserted.
final user = User()
..id = 123
..userName = 'John Doe'
..eMail = 'john@example.com';
await isar.users.put(user);
Deleting the Collection
To delete a collection, specify the ID and execute delete.