mapToUse function

Map<String, dynamic> mapToUse(
  1. Map<String, dynamic> objectMap, {
  2. bool forDB = true,
})

Converts an entity map into a map suitable for SQLite.

  • Values still equal to a ColumnType enum are replaced with null (so they can be stored as SQL NULL).
  • When forDB is true (the default), bool values are coerced to 0/1 and DateTime values to their string representation.

Pass forDB: false when you want a plain Dart map (e.g. to serialize to JSON or display it in the UI).

Implementation

Map<String, dynamic> mapToUse(
  Map<String, dynamic> objectMap, {
  bool forDB = true,
}) {
  return objectMap.map((String key, dynamic value) {
    if (value is ColumnType) return MapEntry(key, null);
    if (forDB && value is bool) return MapEntry(key, value ? 1 : 0);
    if (forDB && value is DateTime) return MapEntry(key, value.toString());
    return MapEntry(key, value);
  });
}