Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 2019-10-02
- class A {
- A(this.item, this.other, this.next);
- final String item;
- final other;
- final int next;
- }
- main(){
- // Setup: We have an object A{String item, var other, int next} inside a list (innerList).
- // This list<A> is inside another list (outerList).
- // Result: The result should be a list of String item. List<String> resultList
- final List<A> innerList = List.generate(10,(i)=> A('Item $i', 'other $i', i) );
- final List<List<A>> outerList = List.generate(10, (_)=> innerList );
- List<String> resultList;
- // 1
- final List<String> resultL =outerList.map<List<String>>((il)=> il.map<String>((A a)=> a.item).toList()).toList().fold([],(a,b)=> a..addAll(b));
- // print(resultList);
- resultList.clear();
- // 2
- resultList = outerList.fold(['kjsdf'], (List<String> a, List<A> b)=> a..addAll(b.map<String>((A i)=>i.item).toList()));
- //print(resultList);
- resultList.clear();
- // 3
- for(List<A> listA in outerList){
- for(A a in listA){
- resultList.add(a.item);
- }
- }
- // 4
- List<String> resultLister() => outerList.fold(
- [],
- (newList, outer) => newList
- ..addAll(
- outer.fold(
- [],
- (innerNewList, inner) => innerNewList
- ..add(
- inner.item,
- ),
- ).toList(),
- ),
- );
- print(resultLister());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement