Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to transform code section into function in Dart / Flutter

I have the following code that does all the work, but I’m having trouble passing it to a function that takes the array as an argument.

void main() 

{
  var pairs = [[9, 5, 1], [3, 6]];
 
  var example= pairs.expand((pair) => pair).toList();

  example.sort();

  print('Result: $example'); 
  
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

type ‘(dynamic) => dynamic’ is not a subtype of type ‘(List) => Iterable’. SO, you can pass with List<List> type.

void main() 

{
  var pairs = [[3, 2, 1], [4, 6, 5], [], [9, 7, 8]];
  getListOfData(pairs);
}

getListOfData(List<List> pairs){
  var desempacotada = pairs.expand((pair) => pair).toList();
  desempacotada.sort();

  print('Result: $desempacotada'); //Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading