【问题标题】:Flutter SEMBAST database: insert new item in a list of an objectFlutter SEMBAST 数据库:在对象列表中插入新项目
【发布时间】:2020-11-08 19:40:48
【问题描述】:

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

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

【问题讨论】:

    标签: flutter dart local-storage sql-update sembast


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 2010-09-22
      • 2019-11-20
      • 2020-02-10
      • 1970-01-01
      相关资源
      最近更新 更多