Flutter-Sembast数据库插入对象列表

时间:2019-05-12 11:56:07

标签: database dart flutter flutter-dependencies

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

我已经创建了一个示例,并将自己定位于以下教程: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,名称:Apple,isSweet:true,离开:[Leaves的实例,Leaves的实例,Leaves的实例]}

错误如下: [ERROR:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常:无效的参数:值'Leaves'类型的实例不支持叶子

2 个答案:

答案 0 :(得分:1)

如前所述,Instance of 'Leaves'不是有效的类型,因此每个Leave也必须进行转换。在没有看到toJson()的情况下很难猜测自己在做什么,但是这样的事情应该可以工作(可以在很大程度上进行优化):

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> toJson() => <String, dynamic>{
        'id': id,
        'name': name,
        'isSweet': isSweet,
        'leaves': leaves?.map((leave) => leave.toJson())?.toList(growable: false)
      };
}

class Leaves {
  final String id;
  final String name;

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

  Map<String, dynamic> toJson() => <String, dynamic>{'id': id, 'name': name};
}

和您的json应该看起来像这样:

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

答案 1 :(得分:0)

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

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'],
    );
  }
}