Objectbox 不以一对多关系存储可为 null 的值
我有这样的一对多关系。
@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()
您可以看到 price
和 weight
不为 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()
afterexecuting Dbservice.pdfbox.put()
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,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论