我已经决定在我的下一个项目中使用Isar数据库,我发现它在处理本地数据时非常有用。
我在其网站上跟踪了快速启动指南。我添加了依赖项。注释了联系人类。运行代码生成器。但是在第四步,我在创建Isar实例时遇到了创建模式的问题。
initIsar() async {
final dir = await getApplicationSupportDirectory();
final isar = await Isar.open(
schemas: [ContactSchema],
directory: dir.path,
inspector: true,
);
}问题是我在哪里输入了ContactSchema,上面说
Undefined name 'ContactSchema'.
Try correcting the name to one that is defined, or defining the name.所以我必须问的问题是,我遵循了指南,但我无法创建一个模式。如何创建一个以使Isar db工作?
更新:
import 'package:isar/isar.dart';
part 'contact.g.dart';
@Collection()
class Contact {
@Id()
int? id;
late String name;
}在添加part 'contact.g.dart'之后,输入这个命令flutter pub run build_runner build,就可以了。
发布于 2022-12-04 21:52:19
为确保答复的完整性:
运行(错误?看看它,它可能会说从第一个命令中删除isar )
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示例集合:
@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();
}建议:(树-x输出)
$ tree -x
.
├── main.dart
└── model
├── timer.dart
└── timer.g.dart
1 directory, 3 files我是怎么用这个的
void main() async {
// ignore: unused_local_variable
final isar = await Isar.open([TimerSchema]);
runApp(const MyApp());
}查看我的计时器应用程序以获得帮助: pranitshah.cyou,我将更新我的网站。
发布于 2022-12-04 22:05:33
运行build_runner命令后,模式类将在MODEL_NAME.g.dart文件中生成。您需要导入该文件才能访问架构类。
发布于 2022-02-10 11:16:35
你好,扎希德,如果你想在你的下一个颤振应用中添加本地db,https://pub.dev上有各种各样的包。
我强烈建议你从https://pub.dev/packages/floor开始使用它很容易学习,重量轻。
试试这个你会喜欢的。以及如何实现你可以谷歌它,这是一个教程,如何使用地板在您的颤音移动应用程序。
https://stackoverflow.com/questions/71062256
复制相似问题