flutterdartiterableruntime-type

Dart - How to get the `runtimeType` of the elements of Iterable without elements?


how do I get to know the runtimeType of the elements that an Iterable is supposed to accept when it has no element?

This is what my fruitless imagination produced so far:

extension MyUglyAndInelegantIterableExtensions on Iterable {
  Type get valueType => 
    this.isNotEmpty ? this.first.runtimeType : dynamic;
}

void main() {
  print('${<int>[1].valueType}');
  print('${<int>[].valueType}');
}

Thank you


Solution

  • extension MyElegantIterableExtensions<T> on Iterable<T> {
      Type get valueType => T;
    }