【问题标题】:Dart - How to get the `runtimeType` of the elements of Iterable without elements?Dart - 如何在没有元素的情况下获取 Iterable 元素的`runtimeType`?
【发布时间】:2020-11-23 08:22:01
【问题描述】:

我如何知道 Iterable 应该是 accept 的元素的 runtimeType 什么时候没有元素?

这是我迄今为止徒劳的想象:

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

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

谢谢

【问题讨论】:

    标签: flutter dart iterable runtime-type


    【解决方案1】:
    extension MyElegantIterableExtensions<T> on Iterable<T> {
      Type get valueType => T;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-28
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      相关资源
      最近更新 更多