1

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?

Pokaboom
  • 1,110
  • 9
  • 28
  • I wrote a test for this using your entites and it works as expected. Are you sure that those properties are in the generated model (e.g. check `lib/objectbox-model.json`)? Maybe you need [to run the generator](https://docs.objectbox.io/getting-started#generate-objectbox-code), e.g. using `flutter pub run build_runner build`. – Uwe - ObjectBox Mar 28 '22 at 12:52
  • @Uwe-ObjectBox yes `.json` file does contain all the properties. In relations, `Pdf` has one but `Product`'s relation list is empty is that expected? And I am so confused because it only doesn't store nullable or a property which has default value and all other are stored – Pokaboom Mar 28 '22 at 13:15
  • @Uwe-ObjectBox I'm using `1.4.1` if that matters – Pokaboom Mar 28 '22 at 13:15
  • @Uwe-ObjectBox can you share anything which you might have assumed I have done it and implemented it in you test because I'm really stuck at this for 3-4 days and I don't really know what I'm doing wrong here in my both questions – Pokaboom Mar 28 '22 at 17:05
  • The changes I made to the ObjectBox Dart example: https://github.com/objectbox/objectbox-dart/compare/so-71637219-value-not-stored You also may want to look at https://docs.objectbox.io/getting-started#object-ids and https://docs.objectbox.io/relations – Uwe - ObjectBox Mar 29 '22 at 06:20

0 Answers0