遵循此
我有一个CurrentUser
实体,如下所示:
import 'package:admin/core/auth/enums/content_type.dart';
import 'package:admin/core/permission/model/permission.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:objectbox/objectbox.dart';
part 'current_user.freezed.dart';
part 'current_user.g.dart';
@freezed
class CurrentUser with _$CurrentUser {
@Entity(realClass: CurrentUser)
factory CurrentUser(
{@Id(assignable: true) int? localId,
@Unique() String? id,
required DateTime createdAt,
required DateTime updatedAt,
required String email,
required String username,
String? mobile,
@Default(false) bool? isEmailConfirmed,
@Default(false) bool? isMobileConfirmed,
@Default(false) bool? isSuperAdmin,
@Default([]) List<ContentType>? accessTo,
DateTime? lastLoginAt,
DateTime? lastLogoutAt,
@Default([]) List<Permission>? permissions}) = _CurrentUser;
factory CurrentUser.fromJson(Map<String, dynamic> json) =>
_$CurrentUserFromJson(json);
}
但这里字段permissions
应该是与模型Permission
的一对多关系。在这种情况下,如何定义关系?
提前谢谢。
您实际上会使用ToMany
(或ToOne
(作为参数类型,因此
required ToMany<Permission> permissions
例如。对于这种情况,ToMany
(或ToOne
(有一个特殊的构造函数,它接受一个项目列表(或者ToOne
的单个项目(。
另请参见objectbox dart中冻结的测试实体:https://github.com/objectbox/objectbox-dart/blob/main/generator/integration-tests/part-partof/lib/frozen.dart