Szynkie
Szynkie

Reputation: 79

How to make a few collections of the same type in Isar db? [Flutter] [Isar]

Is there an option to make more than one collection with objects of the same type in Isar?

Isar doc: https://isar.dev/

how i create single Collection:

import 'package:isar/isar.dart';

@Collection()
class ProgramModel {
  @Id()
  int? id;
  String? title;
}

I want to have second collection of ProgramModel, but i can't add another @Collection() to same model.

Upvotes: 2

Views: 2940

Answers (3)

Ali Gohar
Ali Gohar

Reputation: 1

Just make a new file and implement the ProgramModel @collection there.

Upvotes: 0

Pradeep Singh
Pradeep Singh

Reputation: 41

Tried and can use same collection class in multiple isar instances (dbs).

To start new isar instance:

Isar isarOne = await Isar.open(
  name: 'dbOne',
  schemas: [ContacSchema],
);


Isar isarTwo = await Isar.open(
  name: 'dbTwo',
  schemas: [ContacSchema],
);

In web, it creates 2 DBs inside IndexedDB. On native also it will create 2 isar instances.

Wanted a way to use same collection class to create Multiple Collections in single isar instance, but seems that cannot be done.. so will proceed with multiple isar instances to have multiple tables.

Upvotes: 3

Hithesh chowdary
Hithesh chowdary

Reputation: 1

either you could make a collection with other name of same object model, or else create this model out of the scope of the present isar(with a new isar db)

Upvotes: 0

Related Questions