Reputation: 739
I want to create an extension for a list which would return an in iterable of cumulative values, my approach was like this:-
extension cumulativeList<T> on List<T> {
Iterable<T> get cumulative {
T? value;
return Iterable<T>.generate(this.length, (i) {
if (value == null) value = this[i];
else value = value !+ this[i]; //gives and error that operator '+' is not defined for 'Object'
return value;
});
}
}
So is there a way or a workaround to create a generic extension or function something like this, in which we could check if a certain operator is defined on the object?
Upvotes: 0
Views: 82
Reputation: 739
I just needed to extend T with dynamic, so my extension looks like this:-
extension cumulativeList<T extends dynamic> on List<T> {
Iterable<T> get cumulative {
T? value;
return Iterable<T>.generate(this.length, (i) {
if (value == null) value = this[i];
else value = value + this[i];
return value!;
});
}
}
Upvotes: 1