【发布时间】:2019-09-29 15:01:07
【问题描述】:
我即将在 Flutter 中使用数据库“Sembast”。 具有字符串和 int 等数据类型的简单对象可以正常工作。但是,在使用 Lists 时会出现问题。
我创建了一个示例,并针对以下教程进行了指导:https://resocoder.com/2019/04/06/flutter-nosql-database-sembast-tutorial-w-bloc/ 在我的示例中,有水果和叶子作为对象。一个水果包含一个叶子列表。
class Fruit {
final String id;
final String name;
final bool isSweet;
final List<Leaves> leaves;
...
}
class Leaves {
final String id;
final String name;
...
}
//Create a sample object
var leaveOne = Leaves(id: "1", name: "leaveOne");
var leaveTwo = Leaves(id: "2", name: "leaveTwo");
var leaveThree = Leaves(id: "3", name: "leaveThree");
var leavesList = List<Leaves>();
leavesList.add(leaveOne);
leavesList.add(leaveTwo);
leavesList.add(leaveThree);
var fruit = Fruit(id: "1", name: "Apple", isSweet: true, leaves: leavesList);
_fruitDao.insert(fruit);
// The fruitDao.insert makes following
Future insert(Fruit fruit) async {
await _fruitStore.add(await _db, fruit.toJson());
}
JSON 看起来像这样:{id: 1, name: Apple, isSweet: true, Leaves: [Instance of 'Leaves', Instance of 'Leaves', Instance of 'Leaves']}
错误如下: [错误:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:无效的参数:不支持的“叶子”类型叶子的值实例
【问题讨论】:
标签: database dart flutter flutter-dependencies