I have a collection named 'user' in Isar:
我在Isar有一个名为‘User’的集合:
@Collection()
class UserCollection {
Id id = Isar.autoIncrement;
@Index()
late String mobileNumber;
String? name = '';
final profile = IsarLink<ProfileCollection>();
}
It is linked to another collection named 'profile':
它链接到另一个名为‘Profile’的集合:
@Collection()
class ProfileCollection {
Id id = Isar.autoIncrement;
final IsarLinks<FavoritesMenuCollection> favoriteMenus = IsarLinks<FavoritesMenuCollection>();
@Backlink(to: 'profile')
final user = IsarLink<UserCollection>();
}
And the 'profile' collection itself is connected as a @Backlink
to another collection named 'favoriteMenus':
‘Profile’集合本身作为@Backlink连接到另一个名为‘FavoriteMenus’的集合:
@Collection()
class FavoritesMenuCollection {
Id id = Isar.autoIncrement;
@Backlink(to: 'favoriteMenus')
final profile = IsarLink<ProfileCollection>();
}
I don't have any issues linking 'profile' to 'user', but when I link the 'favoriteMenus' collection to 'profile', the 'profile' property in the 'FavoritesMenuCollection' gets stored as null, and the link is not created. Here's the code I've implemented:
我将‘Profile’链接到‘User’没有任何问题,但当我将‘FavoriteMenus’集合链接到‘Profile’时,‘FavoritesMenuCollection’中的‘Profile’属性被存储为空,并且没有创建链接。以下是我实现的代码:
final profile = ProfileCollection()
..favoriteMenus.addAll(favoriteMenus);
await isar.isarInstance.writeTxn(() async {
await isar.isarInstance.favoritesMenuCollections.putAll(favoriteMenus);
await isar.isarInstance.profileCollections.put(profile);
});
final user = isar.findLast<UserCollection>()!;
await isar.isarInstance.writeTxn(() async {
user
..profile.value = profile
..active = res.user!.active
..verified = true;
await user.profile.save();
});
What could be causing the 'profile' property in 'FavoritesMenuCollection' to be null when linking the collections?
链接集合时,可能是什么原因导致“FavoritesMenuCollection”中的“”Profile“”属性为空?“”
更多回答
优秀答案推荐
In order to link FavoritesMenuCollection
s to your ProfileCollection
you need to call save()
.
Add
为了将FavoritesMenuCollection链接到您的ProfileCollection,您需要调用save()。增列
await profile.favoriteMenus.save();
to the bottom of your first writeTxn()
to fix it.
添加到第一个WriteTxn()的底部以修复它。
See IsarLinks documentation
请参阅IsarLinks文档
更多回答
我是一名优秀的程序员,十分优秀!