I have decided to use Isar database in my next project and I find it much helpful when dealing with local data.
I followed the quickstart guide in its website. I added dependencies. Annotated the contact class. Ran code generator. But at fourth step, I have problem creating schema while creating Isar instance.
initIsar() async {
final dir = await getApplicationSupportDirectory();
final isar = await Isar.open(
schemas: [ContactSchema],
directory: dir.path,
inspector: true,
);
}
The problem is where I typed ContactSchema, it says
Undefined name 'ContactSchema'.
Try correcting the name to one that is defined, or defining the name.
So question I have to ask is, I followed guide but I'm unable to create a schema. How can I create one to make Isar db work?
UPDATE:
import 'package:isar/isar.dart';
part 'contact.g.dart';
@Collection()
class Contact {
@Id()
int? id;
late String name;
}
After adding part 'contact.g.dart', type this command flutter pub run build_runner build and you are good to go.
For completeness of response:
Run (error? look at it, it likely say to remove isar from the first command)
flutter pub add isar isar_flutter_libs
flutter pub add -d isar_generator build_runner
flutter pub run build_runner build # Run every update of the collection
Example collection:
@Collection(accessor: "time")
class Timer {
final Id id = Isar.autoIncrement;
@Index(
unique: true,
replace: true,
)
late final bool isRunning;
late final DateTime dates = DateTime.now();
}
Suggestion: (tree -x output)
$ tree -x
.
├── main.dart
└── model
├── timer.dart
└── timer.g.dart
1 directory, 3 files
For how I'm using this:
void main() async {
// ignore: unused_local_variable
final isar = await Isar.open([TimerSchema]);
runApp(const MyApp());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With