Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't AUTOINCREMENT null on SQLITE? | Autoincrement ID in sqlite flutter

I added this one on onCreate method on SQFLITE. I need to generate an autoincrement id. I printed id but the value is null. How to add autoincrement id using sqflite?

   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)""");
      }

I store data like this. I didn't add id to value. i didn't pass to value to 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("")));
  }

1 Answers

You need to make sure that the id is not added to the map in your toJson() function:

In the official documentation it is handled like this:

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

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

  return map;
}
like image 60
Martin Niederl Avatar answered Oct 17 '25 07:10

Martin Niederl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!