(Flutter,ObjectBox,Freezed) How can add custom type to ObjectBox Entity with freezed

141 Views Asked by At

i have this Entity

@JsonSerializable()
//ignore: must_be_immutable
class UserEntity extends Equatable {
  @Entity(realClass: UserEntity)
  UserEntity({
    required this.id,
    required this.token,
    required this.email,
    required this.organizationId,
    required this.organizationName,
    Map<String, dynamic> firstNameParam = const <String, dynamic>{},
    Map<String, dynamic> lastNameParam = const <String, dynamic>{},
    this.profilePhotoPath,
  })  : _firstName = firstNameParam,
        _lastName = lastNameParam;
  factory UserEntity.fromJson(Map<String, dynamic> json) => _$UserEntityFromJson(json);
  @Id(assignable: true)
  int id;
  int organizationId;
  String organizationName;
  String email;
  String? profilePhotoPath;
  String token;
  @Transient()
  Map<String, dynamic> _firstName;
  @Transient()
  Map<String, dynamic> _lastName;

  String get firstName => json.encode(_firstName);

  set firstName(String value) {
    _firstName = Map.of(jsonDecode(value) as Map<String, dynamic>);
  }

  String get lastName => json.encode(_lastName);

  set lastName(String value) {
    _lastName = Map.of(jsonDecode(value) as Map<String, dynamic>);
  }

  Map<String, dynamic> toJson() => _$UserEntityToJson(this);

  @override
  List<Object?> get props => [
        id,
        organizationId,
        organizationName,
        email,
        profilePhotoPath,
        token,
        firstName,
        lastName,
      ];
}

When i use Freezed

import 'dart:convert';

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:objectbox/objectbox.dart';

part 'user_entity.freezed.dart';
part 'user_entity.g.dart';

@freezed
class UserEntity with _$UserEntity {
  @Entity(realClass: UserEntity)
  const factory UserEntity({
    required int organizationId,
    required String organizationName,
    required String email,
    @Id(assignable: true) @Default(0) int id,
    String? profilePhotoPath,
    @Transient()
    @Default({})
    @JsonKey(name: 'first_name', toJson: _toJson, fromJson: _fromJson)
    Map<String, dynamic> firstName,
    @Transient()
    @Default({})
    @JsonKey(name: 'last_name', toJson: _toJson, fromJson: _fromJson)
    Map<String, dynamic> lastName,
  }) = _UserEntity;
  static Map<String, dynamic> _fromJson(String json) => Map.of(jsonDecode(json) as Map<String, dynamic>);
  static String _toJson(Map<String, dynamic> value) => json.encode(value);
}

i get errors

[WARNING] objectbox_generator:resolver on lib/features/auth/domain/entities/user_entity.dart: Skipping property 'firstName': type 'Map<String, dynamic>' not supported, consider creating a relation for @Entity types (https://docs.objectbox.io/relations), or replace with getter/setter converting to a supported type (https://docs.objectbox.io/advanced/custom-types). [WARNING] objectbox_generator:resolver on lib/features/auth/domain/entities/user_entity.dart: Skipping property 'lastName': type 'Map<String, dynamic>' not supported, consider creating a relation for @Entity types (https://docs.objectbox.io/relations), or replace with getter/setter converting to a supported type (https://docs.objectbox.io/advanced/custom-types).

I tried in various ways, but it was of no use to access the variables from outside constructor

0

There are 0 best solutions below