create<T extends Model> static method
Creates a new record from fromJson.
Prevents mass-assignment of id, uuid, timestamps.
Implementation
static Future<T?> create<T extends Model>(Map<String, dynamic> fromJson) async {
final constructor = _jsonConstructors[T];
if (constructor == null) return null;
try {
fromJson
..remove('id')
..remove('uuid')
..remove('created_at')
..remove('updated_at');
final defaults = {'uuid': _uuid.v4(), 'created_at': DateTime.now().toUtc().toIso8601String(), 'updated_at': DateTime.now().toUtc().toIso8601String()};
final instance = constructor({...fromJson, ...defaults}) as T;
if (await instance.save(disk: .sqlite)) {
return instance;
}
return null;
} catch (e) {
print('SQLite create error: $e');
return null;
}
}