2

想要在 SEMBAST 数据库中的对象列表中插入新项目,

对象 JSON 格式:

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

想插入请假三,“id”:3,“姓名”:请假三,也想更新请假名称,如何添加新请假,更改请假名称?

这是水果和离开模型:

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

如何插入新休假和更新休假?

4

1 回答 1

0

如果我理解正确,因为叶子是一个嵌套列表,您必须完全更新该字段(即您必须自己操作/克隆/修改叶子列表并将其设置在您的 Fruit 对象中)。没有像 firestore 中的 arrayUnion/arrayRemove 操作。

于 2020-07-23T16:27:12.093 回答