How to use List Iterable functions in flutter

2,610

Yes, let's check the following sample code.

List<int> values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    
print(values.skip(5).toList());
//[6, 7, 8, 9]
    
print(values.skip(5).take(3).toList());
//[6, 7, 8]
    
values.skip(5).take(3).map((e) => e.toString()).forEach((element) {print(element);});
//6 7 8
    
String str = values.fold("initialValue",
        (previousValue, element) => previousValue + ", " + element.toString());    
print(str);
//initialValue, 1, 2, 3, 4, 5, 6, 7, 8, 9
    
str = values.join(", ");
print(str);
//1, 2, 3, 4, 5, 6, 7, 8, 9
  • skip(1) skips the first value, 1, in the values list literal.
  • take(3) gets the next 3 values 2, 3, and 4 in the values list literal.
  • map() Returns a new lazy [Iterable] with elements that are created by calling f on each element of this Iterable in iteration order.
  • fork() Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
  • join() Converts each element to a [String] and concatenates the strings.
Share:
2,610
Avdienko Viktor
Author by

Avdienko Viktor

Updated on December 29, 2022

Comments

  • Avdienko Viktor
    Avdienko Viktor 10 months

    I found iterable functions, but I am not sure how I can use. For example, skip, take, map, forEach, fold and join

    Could you give me examples how to use?