let's say I have the user model, I know all fields, id, name, createdAt, and updatedAt from database are not null, but when I want to add new user, id, cratedAt, and updatedAt are null because they will be inserted on database.
Then, my question is what is the best approach to handle this kind of typical case?
1. creating the user model with nullable fields.
class User {
final String? id;
final String name;
final DateTime? createdAt;
final DateTime? updatedAt;
...
}
This maybe the most typical approach, but I have to use ! or check null all the time and feel not good.
2. creating new user model for adding data other than existing one which has no nullable fields.
class User {
final String id;
final String name;
final DateTime createdAt;
final DateTime updatedAt;
...
}
class NewUser {
final String? id;
final String name;
final DateTime? createdAt;
final DateTime? updatedAt;
...
}
This could work on very small size but error prone.
3. generate fields, id, createdAt, and updatedAt on the client side, and use non nullable value on all fields
class User {
final String id;
final String name;
final DateTime createdAt;
final DateTime updatedAt;
...
}
This could work too, but generating id or timestamp on the client side is not good approach.
4. do you have any other recommendation?
I understand there is no silver bullet, and it depends on the situation or preference. I just want to learn the typical approach.
I would appreciate any advice even on the different langs, like java, swift, or kotlin.