Quickstart
Holy smokes, you're here! Let's get started on using the coolest Flutter database out there...
We're going to be short on words and quick on code in this quickstart.
1. Add dependencies
Before the fun begins, we need to add a few packages to the pubspec.yaml
. We can use pub to do the heavy lifting for us.
dart pub add isar:^3.1.8 isar_flutter_libs:^3.1.8 --hosted-url=https://pub.isar-community.dev
dart pub add dev:isar_generator:^3.1.8 --hosted-url=https://pub.isar-community.dev
2. Annotate classes
Annotate your collection classes with @collection
and choose an Id
field.
part 'user.g.dart';
@collection
class User {
Id id = Isar.autoIncrement; // you can also use id = null to auto increment
String? name;
int? age;
}
Ids uniquely identify objects in a collection and allow you to find them again later.
3. Run code generator
Execute the following command to start the build_runner
:
dart run build_runner build
If you are using Flutter, use the following:
flutter pub run build_runner build
4. Open Isar instance
Open a new Isar instance and pass all of your collection schemas. Optionally you can specify an instance name and directory.
final dir = await getApplicationDocumentsDirectory();
final isar = await Isar.open(
[UserSchema],
directory: dir.path,
);
5. Write and read
Once your instance is open, you can start using the collections.
All basic CRUD operations are available via the IsarCollection
.
final newUser = User()..name = 'Jane Doe'..age = 36;
await isar.writeTxn(() async {
await isar.users.put(newUser); // insert & update
});
final existingUser = await isar.users.get(newUser.id); // get
await isar.writeTxn(() async {
await isar.users.delete(existingUser.id!); // delete
});
Other resources
Are you a visual learner? Check out these videos to get started with Isar: