0

I'm trying to use ObjectBox on flutter with theese three Freezed classes:

product_otodb.dart:

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:objectbox/objectbox.dart';
import 'package:sentric/infrastructure/products/otodb/mediagallery_otodb.dart';
import 'package:sentric/infrastructure/products/otodb/variant_otodb.dart';

part 'product_otodb.freezed.dart';

@freezed
class ProductOtoDb with _$ProductOtoDb {
  @Entity(realClass: ProductOtoDb)
  factory ProductOtoDb({
    @Id(assignable: true) required int id,
    required bool validated,
    required String? option1Name,
    required String? option2Name,
    required String? category,
    required String? brand,
    required String? customProductBarcode,
    required String title,
    required String? description,
    required String? descriptionValidated,
    required List<String> barcodes,
    required ToMany<VariantOtoDb> variants,
    required ToOne<MediaGalleryOtoDb> mediaGallery,
    required int? quantity,
    required String ownerShop,
    @Property(type: PropertyType.date) required DateTime updatedDate,
    required double? price,
    required double? priceComparison,
  }) = _ProductOtoDb;
}

mediagallery_otodb.dart:

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

part 'mediagallery_otodb.freezed.dart';

@freezed
class MediaGalleryOtoDb with _$MediaGalleryOtoDb {
  @Entity(realClass: MediaGalleryOtoDb)
  factory MediaGalleryOtoDb({
    @Id(assignable: true) required int id,
    @Unique() required String url,
  }) = _MediaGalleryOtoDb;
}

variant_otodb.dart:

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

part 'variant_otodb.freezed.dart';

@freezed
class VariantOtoDb with _$VariantOtoDb {
  @Entity(realClass: VariantOtoDb)
  factory VariantOtoDb({
    @Id(assignable: true) required int id,
    required String? option1Value,
    required String? option2Value,
    required List<String> barcodes,
    required int? imageIndex,
    required int? quantity,
    required bool? validated,
    required double? price,
    required double? priceComparison,
  }) = _VariantOtoDb;
}

Every time I save on every single file with flutter pub run build_runner watch --delete-conflicting-outputs up and running on a terminal I get this error:

Entity MediaGalleryOtoDb has an ID field 'id' that is not assignable (that usually means it is declared final). This won't work because ObjectBox needs to be able to assign an ID after inserting a new object (if the given ID was zero). If you want to assign IDs manually instead, you can annotate the field 'id' with `@Id(assignable: true)`. Otherwise please provide a setter or remove the `final` keyword.

This happens not just on MediaGalleryOtoDb but also on VariantOtoDb and ProductOtoDb.

Can you please provide me an explanation about this errors?

Actually, if I remove assignable: true or replace it with assignable: false into one of the three (or into the whole classes, sometimes) it does the trick and it works, but I really don't understand why.

Thanks!

  • Please also check https://github.com/objectbox/objectbox-dart/issues/229, it may give you additional info and alternative approaches. – Markus Junginger Apr 22 '22 at 08:09
  • Can't spot any obvious error. We have a test with multiple freezed classes that runs fine: https://github.com/objectbox/objectbox-dart/blob/main/generator/integration-tests/part-partof/lib/frozen.dart (also linked from above issue). If you couldn't figure this out, yet, please create a GitHub issue and attach a small project for us to reproduce. – Uwe - ObjectBox Apr 25 '22 at 06:01

0 Answers0