为什么SQFLITE上的AUTOINCREMENT不为null?

时间:2019-04-28 04:33:31

标签: dart flutter sqflite

我在SQFLITE的onCreate方法上添加了此代码。我需要生成一个自动增量ID。我打印了id,但该值为null。如何使用sqflite添加自动增量ID?

   await db.execute(
            """CREATE TABLE submitRequestTable(id INTEGER PRIMARY KEY AUTOINCREMENT,equipmentId INTEGER, woDescriptionId INTEGER, details STRING, priorityId INTEGER,
            workTypeId INTEGER, sourceId INTEGER, filename STRING, isOffline INTEGER, systemDate STRING, username STRING, subItemId INTEGER, status STRING)""");
      }

我这样存储数据。我没有在值中添加ID。我没有将值传递给id。

  storeSubmitRequest(
      BuildContext context,
      int equipmentId,
      int woDescriptionId,
      String details,
      int priorityId,
      int workTypeId,
      int sourceId,
      String filename,
      int isOffline,
      String systemDate,
      String username,
      int subItemId,
      String status) async {
    var db = await db1;
    Batch batch = db.batch();
    var data = SubmitRequestModel(
        equipmentId: equipmentId,
        woDescriptionId: woDescriptionId,
        details: details,
        priorityId: priorityId,
        workTypeId: workTypeId,
        sourceId: sourceId,
        filename: filename,
        isOffline: isOffline,
        systemDate: systemDate,
        username: username,
        subItemId: subItemId,
        status: status);
    batch.insert(
      'submitRequestTable',
      data.toJson(),
      // conflictAlgorithm: ConflictAlgorithm.replace,
    );
    await batch.commit();
    Navigator.push(
        context, MaterialPageRoute(builder: (context) => ListPage("")));
  }

2 个答案:

答案 0 :(得分:1)

您需要从Map中删除空值,例如:

  Map<String, dynamic> toJson() {
    final map = {
      "id": this.id,
      "type": this.type.value,
      "count": this.count,
      "currency": this.currency,
      "category": this.category,
      "comment": this.comment,
      "timestamp": this.timestamp
    };

    map.removeWhere((key, value) => value == null);

    return map;
  }

答案 1 :(得分:0)

您需要确保您的toJson()函数中未将ID添加到地图中:

在官方文档中,其处理方式如下:

Map<String, dynamic> toMap() {
  var map = <String, dynamic>{
    columnTitle: title,
    columnDone: done == true ? 1 : 0
  };

  if (id != null) map['id'] = id;

  return map;
}