3

我正在尝试在 ObjectBox 框中存储地图列表,但由于我是 ObjectBox 的新手,我不明白如何存储自定义数据类型。

我试图存储的数据(结构看起来像这样)

name: 'name',
total: 100,
refNo: 00,
listOfProds: [{Map1},{Map2},{Map3},...]//<-Problem

我试过了

@Entity()
class ABC{
  int id;
  String name;
  int total;
  int refNo;
  List<Map>? products;

  ABC({
  this.id=0,
  required this.name,
  required this.total,
  required this.refNo,
  //required this.products, <-This will throw an error since List is not supported
  });
}

//Adding into DB
void addIntoDB(){
  late ABC _abc;
  _abc = ABC(
   name: 'name',
   total: 100,
   refNo: 00,
   //How can I assign the 'list of maps here' or any other ways?
  );
  _store.box<ABC>().put(_abc);
}
4

1 回答 1

1

参考Custom Types Documentation,结果发现以原始格式存储列表是不可能的。(它仍在功能请求中)

因此,请尝试将该列表转换为 JSON 格式,这将产生一个字符串。

String listInJson = json.encode(theList);

现在把它放进盒子里:

@Entity()
class ABC{
  int id;
  String name;
  int total;
  int refNo;
  String products;

  ABC({
  this.id=0,
  required this.name,
  required this.total,
  required this.refNo,
  required this.products,
  });
}

//Adding into DB
void addIntoDB(){
  late ABC _abc;
  _abc = ABC(
  name: 'name',
  total: 100,
  refNo: 00,
  products: listInJson,//<- That's how you do it.
  );
_store.box<ABC>().put(_abc);
}

检索时,只需json.decode(products).

于 2022-02-01T12:37:58.277 回答