Realm内支持的基础类型
intdoubleboolStringDateTime
其中DateTime的使用
@RealmModel()
class _Vehicle {
@PrimaryKey()
late String nickname;
late DateTime dateLastServiced;
}
//使用方法
// Create a Realm object with date in UTC, or convert with .toUtc() before storing
final subaruOutback = realm.write<Vehicle>(() {
return realm.add(Vehicle('Subie', DateTime.utc(2022, 9, 18, 12, 30, 0)));
});
final fordFusion = Vehicle('Fuse', DateTime(2022, 9, 18, 8, 30, 0).toUtc());
realm.write(() {
realm.add(fordFusion);
});
// When you query the object, the `DateTime` returned is UTC
final queriedSubaruOutback =
realm.all<Vehicle>().query('nickname == "Subie"')[0];
// If your app needs it, convert it to Local() or the desired time zone
final localizedSubieDateLastServiced =
queriedSubaruOutback.dateLastServiced.toLocal();
Realm还支持以下的类型
RealmList
Realm对象可以包含任何支持的数据类型的列表。Realm使用RealmList数据类型来存储数据。
当你将RealmObjects作为项目包含在RealmList中时,它表示的是一对多关系。
从数据库中删除对象会将其从存在的realmlist中删除。因此,RealmObject类型的RealmList永远不会包含null值。另外,一个RealmList可以包含对同一个RealmObject的多个引用。
基本类型的RealmList可以包含null值。如果你不想在列表中允许null值,那么要么在列表声明中使用非空类型(例如,使用list 而不是list <int?>)。
RealmList是可变的,您可以在写事务中添加和删除RealmList上的元素。
创建RealmList
@RealmModel()
class _Player{
@PrimaryKey()
late String username;
late List<_Item> inventory;
late List<String> traits;
}
@RealmModel()
class _Item{
@PrimaryKey()
late String name;
late String description;
}
使用RealmList
final config = Configuration.local([Player.schema, Item.schema]);
final realm = Realm(config);
//写入一个Player
final artemis = realm.write(() => realm.add(Player('Art3mis', inventory: [
Item('elvish sword', 'sword forged by elves'),
Item('body armor', 'protects player from damage'),
], traits: [
'brave',
'kind'
])));
//使用RealmList的方法获取数据
RealmList<String> traits = artemis.traits;
final brave = traits.firstWhere((element) => element=='brave');
final elvishSword = artemis.inventory.where((element) => element.name=='elvish sword').first;
//使用Realm的语句方式获取数据
final playersWithBodyArmor = realm.query<Player>("inventory.name==$0",['body armor']);
realm.close();
RealmSet
RealmSet 可以包含除另一个集合之外的任何受支持的数据类型的集合。Realm使用RealmSet数据类型来存储数据。在RealmSet集合中,所有值都是唯一的。RealmSet扩展了原生的Dart集数据类型,提供了额外的特定于域的属性和方法。
当您将RealmObjects作为项目包含在RealmSet中时,它表示的是一对多关系。
RealmSet是可变的,您可以在写事务中添加和删除RealmSet中的元素。
在定义RealmSet时:
一组基本类型可以定义为可空类型或不可空类型。例如,Set<int>和Set<int?>在Realm schema中有效。
一组RealmObject和RealmValue类型只能是不可空的。例如,设置<RealmValue>是有效的,设置<RealmValue?>无效。
在定义集合时,不能定义默认值。例如,设置mySet ={0,1,2}是无效的。
创建RealmSet
@RealmModel()
class _RealmSetExample {
late Set<String> primitiveSet;
late Set<int?> nullablePrimitiveSet;
late Set<_SomeRealmModel> realmObjectSet;
}
@RealmModel()
class _SomeRealmModel {
late ObjectId id;
}
使用RealmSet
// 通过基础类型创建
final setExample = RealmSetExample(
primitiveSet: {'apple', 'pear'},
nullablePrimitiveSet: {null, 2, 3},
realmObjectSet: {SomeRealmModel(ObjectId())});
// 添加一个对象进数据库
realm.write(() => realm.add(setExample));
// 一旦你将Sets加入Realm, 它的类型将变为RealmSet
RealmSet primitiveSet = setExample.primitiveSet;
//在写事务中修改RealmObjects的realmset
realm.write(() {
// 使用RealmSet.Add()向RealmSet添加元素
setExample.realmObjectSet.add(SomeRealmModel(ObjectId()));
// 使用RealmSet.Remove()从RealmSet中删除元素
setExample.primitiveSet.remove('pear');
});
//使用RealmSet.contains()检查RealmSet是否包含元素
if (setExample.primitiveSet.contains('apple')) {
print('Set contains an apple');
}
//使用Realm查询语言查询Realm
final results =
realm.query<RealmSetExample>('$0 IN nullablePrimitiveSet', [null]);
//使用RealmSet.length检查RealmSet中的元素数量
print(setExample.primitiveSet.length);
RealmResults
RealmResults集合表示查询操作的延迟求值结果。与RealmList不同,results是不可变的:你不能在results集合上添加或删除元素。这是因为结果集合的内容是由针对数据库的查询决定的。有关查询Realm数据库的更多信息,请参阅读取操作
Realm.all()和Realm.query()返回RealmResults。
RealmResults<Player> players = realm.all<Player>();
RealmResults<Player> bravePlayers =
realm.query<Player>('ANY traits == $0', ['brave']);
其他支持的数据类型
ObjectId
ObjectId是mongodb特有的12字节唯一值,可以用作对象的标识符。ObjectId是可索引的,可以用作主键。
要将属性定义为ObjectId,需要在对象模型中将其类型设置为ObjectId。
@RealmModel()
class _ObjectIdPrimaryKey {
@PrimaryKey()
late ObjectId id;
}
final id = ObjectId();
final object = ObjectIdPrimaryKey(id);
UUID
UUID(通用唯一标识符)是一个16字节的唯一值。你可以使用UUID作为对象的标识符。uuid是可索引的,可以将它们用作主键。
使用UUID代替ObjectId
一般来说,你可以对任何作为唯一标识符的字段使用UUID。
如果迁移的数据没有存储在MongoDB中,使用UUID可能特别有用,因为你的对象的唯一标识符很可能已经是UUID类型。
另外,对于MongoDB中已经存在的数据集合,使用ObjectId可能会很有用。
@RealmModel()
class _UuidPrimaryKey {
@PrimaryKey()
late Uuid id;
}
final myId = Uuid.v4();
final object = UuidPrimaryKey(myId);