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

Flutter : Copy data from one dart list to another list by searching with element

The list _data contain data from the csv file.

  List<List<dynamic>> _data = [];
  String key = "39";
  List<List<dynamic>> _tempdata = [];

  void _loadCSV() async {
    final _rawData = await rootBundle.loadString("assets/mycsv.csv");
    List<List<dynamic>> _listData =
        const CsvToListConverter().convert(_rawData);
    setState(() {
      _data = _listData;
    });
}

mycsv.csv data

id,Name,Num,Batch
15,JERRY,PH123,G9
27,Tom,PH129,G8
39,Oggy,PH124,G9
45,Jack,PH125,G10 

I need to
Get the data where id == key(search element) from the list "_data" and store it in new list named "_tempdata"

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

>Solution :

You can use the where method of the List class to filter the elements of the _data list that match the condition you specified and store the result in a new list _tempdata. Here’s an example of how you can implement this in your code:

_tempdata = _data.where((element) => element[0] == key).toList();
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