Enumerate or map through a list with index and value in Dart

后端 未结 8 2142
难免孤独
难免孤独 2020-12-08 01:28

In dart there any equivalent to the common:

enumerate(List) -> Iterator((index, value) => f)
or 
List.enumerate()  -> Iterator((index, value) =>          


        
8条回答
  •  太阳男子
    2020-12-08 02:12

    For convenience you can use this extension method.

    extension CollectionUtil on Iterable  {
    
      Iterable mapIndexed(E Function(int index, T item) transform) sync* {
        var index = 0;
    
        for (final item in this) {
          yield transform(index, item as T);
          index++;
        }
      }
    }
    

提交回复
热议问题