objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

@Transient does not work with freezed #565

Closed mstfkhazaal closed 3 months ago

mstfkhazaal commented 7 months ago

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).

greenrobot-team commented 7 months ago

Double post on Stack Overflow, please avoid that, it takes away time from actually addressing issues.

As said there:

These are warnings that these properties are skipped/ignored, not errors. Then again, it's odd that ObjectBox does not pick up the @Transient() annotation. Is it from the objectbox package?

mstfkhazaal commented 7 months ago

Sorry, thanks for the warning Did you cancel the question on the Stack Overflow? but he didn't just pick up when the parameter accept null value , Map<String, dynamic>? firstName

greenrobot-team commented 7 months ago

OK, turns out this is because freezed will create a getter for non-required properties. And the generator currently does not recognize annotations on getters, see #392.

greenrobot-team commented 3 months ago

This should be fixed with the next release as #392 will be possible.