Objectbox 不以一对多关系存储可为 null 的值

发布于 2025-01-17 04:09:43 字数 3885 浏览 1 评论 0原文

我有这样的一对多关系。

@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];
  }
}

现在我如何将数据添加到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);
}

现在在执行Dbservice.pdfbox.put()

在此处输入图像描述

执行后Dbservice.pdfbox.put()

在此处输入图像描述

您可以看到 priceweight 不为 null,而且 RelInfo 也不是null(它有一个参数为 null)

现在,当我从数据库获取数据时,我得到的所有数据与我输入的数据相同,除了可为空的变量或包含默认值的变量。它们是价格重量和'weightType`。

您可以在这里看到,

在此处输入图像描述

最后,这就是我创建 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();
  }
}

main() 中调用 create() code>

如果我把 final pdf = ToOne() 在产品模型中,然后我之前问的问题又出现了。

这是哪个-> 如何在objectbox的框中多次添加相同的对象?< /a>

现在你们能告诉我为什么只有可为空的/具有默认值的才没有存储在 objectbox 数据库中吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文