首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将地图列表存储在ObjectBox框中?

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

Stack Overflow用户
提问于 2022-02-01 09:39:08
回答 1查看 645关注 0票数 3

我试图在ObjectBox框中存储一个映射列表,但是由于我是ObjectBox新手,所以我不知道如何存储自定义数据类型。

我试图存储的数据(结构如下所示)

代码语言:javascript
运行
复制
name: 'name',
total: 100,
refNo: 00,
listOfProds: [{Map1},{Map2},{Map3},...]//<-Problem

我试过

代码语言:javascript
运行
复制
@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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-01 12:37:58

引用自定义类型文档,结果发现以原始格式存储列表是不可能的。(仍处于功能要求中)

因此,请尝试将该列表转换为格式,这将导致一个字符串.

代码语言:javascript
运行
复制
String listInJson = json.encode(theList);

然后把它放进盒子里

代码语言:javascript
运行
复制
@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)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70938311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档