Realm
1 安装
flutter pub add realm
pubspec.yaml -> dependencies -> realm: latest_version
image.png2 import
import 'package:realm/realm.dart';
part 'filename.g.dart'
@RealmModel
class _ClassName {
}
2.1 示例
import 'package:realm/realm.dart';
part 'car.g.dart';
@RealmModel()
class _Car {
@PrimaryKey()
late final String make;
late String? model;
late int? miles;
}
3 generate
根据@RealmModel 声明的对象生成 对应的class文件
flutter pub run realm generate
4 Configuration
Realm 的配置信息
static LocalConfiguration local(
List<SchemaObject> schemaObjects, {
InitialDataCallback? initialDataCallback,
int schemaVersion = 0,
String? fifoFilesFallbackPath,
String? path,
List<int>? encryptionKey,
bool disableFormatUpgrade = false,
bool isReadOnly = false,
ShouldCompactCallback? shouldCompactCallback,
MigrationCallback? migrationCallback,
int? maxNumberOfActiveVersions,
bool shouldDeleteIfMigrationNeeded = false,
}
5 realm.write
进行数据更新都需要在write的闭包内执行
realm.write(() {
// 增加
// 更新
// 删除
})
6. 增加
realm.write(() {
// 添加某一个
realm.add(object);
// or 数组
// realm.add(objects);
});
7 更新
.write(() {
/// model 更新
})
8 删除
realm.write(() {
realm.delete(object);
// or
// realm.deleteAll<Type>();
});
网友评论