flutterdartflutter-moor

The parameter 'id' is required. when id is Autoincrement


When I try to create a data object to save in my database, it is giving me a warning saying that the id is required. However, the is should be automatically created by moor when added to the database, as it is marked as autoincrement. My table code is

class Countdowns extends Table {
  IntColumn get id => integer().autoIncrement()();
  TextColumn get name => text().withLength(min: 1, max: 100)();
  DateTimeColumn get date => dateTime()();
}

Is this warning safe to ignore? If not, what should I do about it? My code to create the data object is

countdown = Countdown(date: initialDate);

I will fill in the name field when the user enters a name for the countdown.


Solution

  • According to Moor documentation- Columns with a default value (either through autoIncrement or by using default), are still marked as @required in generated data classes. This is because they are meant to represent a full row, and every row will have those values. Use companions when representing partial rows, like for inserts or updates.

    So you can use Companions for inserting/updating data. Companions are also auto-generated and you can use TableNameCompanion.insert() constructor to insert data.

    You can read more on the official moor docs. https://moor.simonbinder.eu/docs/getting-started/advanced_dart_tables/

    There is also an official issue on Moor github where the author explains the reason for having @required on autoincrement. https://github.com/simolus3/moor/issues/548