-
Notifications
You must be signed in to change notification settings - Fork 224
Closed
Labels
featureProposed language feature that solves one or more problemsProposed language feature that solves one or more problemsstate-duplicateThis issue or pull request already existsThis issue or pull request already exists
Description
Hi.
I have problem
Future<List<DBParseable>> getAllData(String tableName) async {
final List<Map<String, dynamic>> maps = await _db.query(tableName);
switch (tableName) {
case Schedule.table:
return List.generate(maps.length, (i) {
return Schedule.fromJson(maps[i]);
});
case Mark.table:
return List.generate(maps.length, (i) {
return Mark.fromJson(maps[i]);
});
case Profile.table:
return List.generate(maps.length, (i) {
return Profile.fromJson(maps[i]);
});
}
return null;
}
as above function. If I want to parse object by factory .fromJson
I can't use generic type because abstract class don't support factory without body.
If abstract class support factory I can do as below
abstract class DBParseable {
String get tableName;
Map<String, dynamic> toJson();
factory fromJson(Map<String,dynamic> json);
}
Future<List<T>> getAllData<T extends DBParseable>(String tableName) async {
final List<Map<String, dynamic>> maps = await _db.query(tableName);
return List.generate(maps.length, (i) {
return T.fromJson(maps[i]);
});
return null;
}
Metadata
Metadata
Assignees
Labels
featureProposed language feature that solves one or more problemsProposed language feature that solves one or more problemsstate-duplicateThis issue or pull request already existsThis issue or pull request already exists