Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 颤振-Sembast数据库插入对象列表_Database_Dart_Flutter_Flutter Dependencies - Fatal编程技术网

Database 颤振-Sembast数据库插入对象列表

Database 颤振-Sembast数据库插入对象列表,database,dart,flutter,flutter-dependencies,Database,Dart,Flutter,Flutter Dependencies,我将在flifter中使用数据库“Sembast”。 数据类型为string和int的简单对象工作正常。但是,使用列表时会出现问题 我创建了一个示例,并在以下教程中介绍了自己: 在我的示例中,有水果和树叶作为对象。水果里有一系列的叶子 类水果{ 最终字符串id; 最后的字符串名; 最后的布尔伊斯韦特; 最后的名单; ... } 课堂假期{ 最终字符串id; 最后的字符串名; ... } //创建一个示例对象 var LEVEONE=叶(id:“1”,名称:“LEVEONE”); var leav

我将在flifter中使用数据库“Sembast”。 数据类型为string和int的简单对象工作正常。但是,使用列表时会出现问题

我创建了一个示例,并在以下教程中介绍了自己: 在我的示例中,有水果和树叶作为对象。水果里有一系列的叶子

类水果{
最终字符串id;
最后的字符串名;
最后的布尔伊斯韦特;
最后的名单;
...
}
课堂假期{
最终字符串id;
最后的字符串名;
...
}
//创建一个示例对象
var LEVEONE=叶(id:“1”,名称:“LEVEONE”);
var leaveTwo=叶子(id:“2”,name:“leaveTwo”);
var leaveThree=叶子(id:“3”,名称:“leaveThree”);
var leavesList=List();
leavesList.添加(leaveOne);
添加(leaveTwo);
叶列表。添加(叶三);
var水果=水果(id:“1”,名称:“苹果”,isSweet:true,叶子:leavesList);
_水果刀。插入(水果);
//insert生成以下命令
未来插入(水果)异步{
wait_fruitStore.add(wait_db,fruit.toJson());
}
JSON看起来是这样的:{id:1,name:Apple,isSweet:true,leaves:[实例'leaves',实例'leaves',实例'leaves']}

错误如下:
[错误:flatter/lib/ui/ui\u dart\u state.cc(148)]未处理的异常:无效参数:“Leaves”的值实例不支持类型Leaves

如上所述,“Leaves”的
实例
不是有效类型,因此每个
Leave
也必须转换。如果没有看到
toJson()

类水果{
最终字符串id;
最后的字符串名;
最后的布尔伊斯韦特;
最后的名单;
水果({this.id,this.name,this.isSweet,this.leaves});
映射到JSON()=>{
“id”:id,
“名称”:名称,
“isSweet”:isSweet,
“leaves”:leaves?.map((leave)=>leave.toJson())?.toList(可增长:false)
};
}
课堂假期{
最终字符串id;
最后的字符串名;
叶子({this.id,this.name});
映射到JSON()=>{'id':id,'name':name};
}
您的json应该如下所示:

{
  "id": "1",
  "name": "Apple",
  "isSweet": true,
  "leaves": [
    {
      "id": "1",
      "name": "leaveOne"
    },
    {
      "id": "2",
      "name": "leaveTwo"
    },
    {
      "id": "3",
      "name": "leaveThree"
    }
  ]
}

除了@alextk answer之外,这里还有一个示例,它可以在不生成任何代码或库的情况下进行转换

class Fruit {
  final String id;
  final String name;
  final bool isSweet;
  final List<Leaves> leaves;

  Fruit({this.id, this.name, this.isSweet, this.leaves});

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'isSweet': isSweet,
      'leaves': leaves.map((leave) => leave.toMap()).toList(growable: false)
    };
  }

  static Fruit fromMap(Map<String, dynamic> map) {
    return Fruit(
      id: map['id'],
      name: map['name'],
      isSweet: map['isSweet'],
      leaves: map['leaves'].map((mapping) => Leaves.fromMap(mapping)).toList().cast<Leaves>(),
    );
  }
}

class Leaves {
  final String id;
  final String name;

  Leaves({this.id, this.name});

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
    };
  }

  static Leaves fromMap(Map<String, dynamic> map) {
    return Leaves(
      id: map['id'],
      name: map['name'],
    );
  }
}
类水果{
最终字符串id;
最后的字符串名;
最后的布尔伊斯韦特;
最后的名单;
水果({this.id,this.name,this.isSweet,this.leaves});
映射toMap(){
返回{
“id”:id,
“名称”:名称,
“isSweet”:isSweet,
“leaves”:leaves.map((leave)=>leave.toMap()).toList(可增长:false)
};
}
静态水果地图(地图地图){
还果(
id:map['id'],
名称:map['name'],
isSweet:map['isSweet'],
叶子:映射['leaves'].map((映射)=>leaves.fromMap(映射)).toList().cast(),
);
}
}
课堂假期{
最终字符串id;
最后的字符串名;
叶子({this.id,this.name});
映射toMap(){
返回{
“id”:id,
“名称”:名称,
};
}
静态叶子来自地图(地图地图){
复叶(
id:map['id'],
名称:map['name'],
);
}
}

感谢您的回复!我正在使用“json_serializable”包。因此,这将为函数
toJson()
fromJson()
生成代码。如果我编辑这个生成的文件(通常你不应该这样做),然后粘贴你的代码来映射叶子(
'leaves':leaves?.map((leave)=>leave.toJson())?.toList(growtable:false)
)-它工作得很好。你可以很好地使用工厂方法,而不是使用静态fromMap方法