I'm learning flutter and I'm trying to turn requests results into more convenient objects, so I created the basic schema of a response (which is not supposed to be instantiated anytime) :
class BaseModel {
final String id;
final DateTime createdAt;
final DateTime updatedAt;
final String databaseId;
final String collectionId;
BaseModel(
{required this.id,
required this.createdAt,
required this.updatedAt,
required this.databaseId,
required this.collectionId});
factory BaseModel.fromMap(Map<String, dynamic> map) {
return BaseModel(
id: map['\$id'].toString(),
collectionId: map['\$collectionId'].toString(),
createdAt: DateTime.tryParse(map['\$createdAt'].toString())!,
updatedAt: DateTime.tryParse(map['\$updatedAt'].toString())!,
databaseId: map['\$databaseId'].toString());
}
}
I'd like to use this basis to create all the other objects I'll need, but I feel like I'm forced to write some useless code everytime I extend the class :
class ProfilePictureModel extends BaseModel {
final String userId;
final Uri pfp;
ProfilePictureModel(
{required super.id,
required super.createdAt,
required super.updatedAt,
required super.databaseId,
required super.collectionId,
// everything above that has been written already in super class
required this.userId,
required this.pfp});
factory ProfilePictureModel.fromMap(Map<String, dynamic> map) {
return ProfilePictureModel(
id: map['\$id'].toString(),
collectionId: map['\$collectionId'].toString(),
createdAt: DateTime.tryParse(map['\$createdAt'].toString())!,
updatedAt: DateTime.tryParse(map['\$updatedAt'].toString())!,
databaseId: map['\$databaseId'].toString(),
// everything above that has been written already in super class
userId: map['user_id'].toString(),
pfp: Uri.dataFromString(map['pfp_url'].toString()),
);
}
}
So here's my question eventually : how can I take advantage of super constructor and factory in this situation ? I felt stuck with setting BaseModel
abstract
. Maybe am I going on the wrong way ? Thanks already peeps !