Reputation: 7208
I want to pass a Type parameter to my function. Is that possible?
For example:
List getSomething(Type typeToReturn) {
return List<dynamic, typeToReturn>();
}
My code:
List<charts.Series> getSeriesData(Type typeToReturn) {
List<DateTime> dates = getDaysInBetween(startTime, DateTime.now());
List<Map> data = [];
dates.forEach((DateTime date) => data.add({
'date': '${date.day.toString()}.${date.month.toString()}',
'score': _getTotalScoreInSpecificDate(date)
}));
return [
new charts.Series<dynamic, typeToReturn>(
id: 'Score',
colorFn: (_, __) => charts.MaterialPalette.blue.shadeDefault,
domainFn: (data, _) => data['date'],
measureFn: (data, _) => data['score'],
data: data,
)
];
}
Upvotes: 0
Views: 1226
Reputation: 3229
You want to do something like this using generics, completely dropping your typeToReturn
parameter:
List<charts.Series> getSeriesData<T>() {
// ...
return [
new charts.Series<dynamic, T>(
// ...
)
];
}
Also, I don't believe you can use a Type
object dynamically as a type parameter. I think you can using dart:mirrors
, but that library isn't compatible with Flutter and isn't really maintained anymore.
Upvotes: 3
Reputation: 2818
Yes, you can use Generics, so the function will be something like this:
List<dynamic, T> getSomething<T>(T typeToReturn) {
return List<dynamic, T>();
}
Upvotes: 1