Home > Back-end >  how to convert Flutter List<List> to List
how to convert Flutter List<List> to List

Time:05-25

// Here Is my Code

List data = [[0,1],[0,1],[0,1]];
List requiredList = [1,1,1];

// please give me some suggestions as soon as possible

CodePudding user response:

Simply loop through the data and add the item in index 1 into output.

void main() async {
  List data = [[0,1],[0,1],[0,1]];
  List output = [];

  for(final d in data){
    output.add(d[1]);
  }

  print(output);
}

Output:

[1, 1, 1]

CodePudding user response:

To get the second value of the elements in the first list as second list you can do this:

List requiredList = data.expand((e)=>[e[1]]).toList();

CodePudding user response:

Your data contains lists, i.e to access data of list you have to pass index of list, i.e 0 1 2 etc.

List data = [[0,1],[0,1],[0,1]];
               ^     ^     ^
               |     |     |
               0     1     2

Above mentioned are index of each sub-list

So if you want to access data from sub-list, call the main list followed by index of sub-list and index number of elements of sub-list.

data[0][1]    //this will give an output of 1
 ^   ^  ^
 |.  |. |
 *. **. ***

*. => list containing lists here

** => Index of sub-list

*** => index of element in list

Check this video here to get more clarity of concept. I've attached a video related to python but same things apply in dart/flutter as well.

CodePudding user response:

Are you asking about how to merge all three lists in the variable data to a single list? If so, I believe the merged list should be [0,1,0,1,0,1]!

(Also, I had to post this as an answer because I don't have enough reputation to comment.)

Whoever downvoted this, can you PLEASE state why? I'd REALLY appreciate some feedback. I've gotten several downvotes and NONE of them have explanations as to why.

  • Related