Objectbox doesn't store nullable value in one to many relation

627 views Asked by At

I have this one to many relation which looks like this.

@Entity()
class Product {
  @Id()
  int id;
  String name;
  String category;
  int categoryId;
  WeightType weightType;
  double? price;
  double weight;
  bool isRefunded;

  Product({
    this.id = 0,
    required this.name,
    required this.category,
    required this.categoryId,
    this.weightType = WeightType.kg,
    this.price,
    this.weight = 0,
    this.isRefunded = false,
  });

  Product copy() => Product(
        name: name,
        category: category,
        id: id,
        price: price,
        weight: weight,
        weightType: weightType,
        categoryId: categoryId,
        isRefunded: isRefunded,
      );

  String getInfo(int index) {
    switch (index) {
      case 1:
        return name;
      case 2:
        return price.toString();
      case 3:
        return weight.toString() + ' ' + weightType.getName();
      case 4:
        if (weightType == WeightType.kg) {
          return (price! * weight).toString();
        } else {
          return (price! * (weight / 1000)).toString();
        }
      default:
        return '';
    }
  }
}
@Entity()
class Pdf {
  @Id()
  int id;
  Uint8List pdfData;
  final String customerName;
  @Property(type: PropertyType.date)
  final DateTime purchaseDate;
  ToMany<Product> products; //<----------- the relation
  double totalAmount;
  PaymentStatus paymentStatus;
  @Property(type: PropertyType.date)
  DateTime? updateDate;

  Pdf({
    this.id = 0,
    required this.pdfData,
    required this.purchaseDate,
    required this.customerName,
    required this.totalAmount,
    required this.products,
    this.paymentStatus = PaymentStatus.unPaid,
    this.updateDate,
  });

  int get status => paymentStatus.index;

  set status(int value) {
    paymentStatus = PaymentStatus.values[value];
  }
}

now how I am adding data to db,

void addToDb(Bill bill){
final billPdf = Pdf(
      pdfData: pdf,
      purchaseDate: DateTime.now(),
      customerName: bill.customerName,
      totalAmount: bill.totalAmount,
      paymentStatus: bill.paymentStatus,
      products: obj.ToMany(items: bill.products) //<------- this is the product list
    );
    DBService.pdfBox.put(billPdf,mode: obj.PutMode.insert);
}

now before executing Dbservice.pdfbox.put()

enter image description here

afterexecuting Dbservice.pdfbox.put()

enter image description here

you can see that price and weight are not null and also RelInfo is also not null(it has one parameter which is null)

Now when I fetch data from db, I get all the data same as I put except the nullable variables or a variable which contains default value. which are price, weight and 'weightType`.

which you can see here,

enter image description here

Lastly, this how I created objectbox store,

class DBService {
  static late final Store store;

  static late final Box<Product> productBox;

  static late final Box<Category> categoryBox;

  static late final Box<Pdf> pdfBox;

  DBService._();

  static Future<void> create() async {
    store = await openStore();
    productBox = store.box();
    categoryBox = store.box();
    pdfBox = store.box();
  }
}

calling create() in main()

If I put final pdf = ToOne<Pdf>() in product model then question which I asked earlier happens all over again.

which is this -> how to add same object in objectbox's box multiple times?

Now can you guys tell me why only nullable/which has default values are not getting stored in objectbox db?

0

There are 0 answers