如何在 ObjectBox 框中存储地图列表?

How to store a list of maps in ObjectBox box?

我试图在 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);
}

参考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)