Arud
Arud

Reputation: 151

How to convert List<List<Map<String, String>>> into List<List<CustomObject>> in dart

I want to convert a List<List<Map<String, String>>> into List<List> a custom class, how to achieve this in dart.

How to convert this

List<List<Map<String, String>>> = [
      {
        "course_name": "Estimation & Quantity Surveying",
        "credit": "4",
        "hours": "40",
      },
      {
        "course_name": "IDP - Industrial Design Project phase II",
        "credit": "4",
        "hours": "40",
      }
    ],
    [
      {
        "course_name": "Data Base Management System",
        "credit": "4",
        "hours": "40",
      },
      {
        "course_name": "Estimation & Quantity Surveying",
        "credit": "4",
        "hours": "40",
      },
    ],
  ];

into

List<List<StudentTimeTable>>

This is my custom class

class StudentTimeTable{
  final String courseName;
  final String credit;
  final String hours;
}

Upvotes: 0

Views: 51

Answers (1)

julemand101
julemand101

Reputation: 31199

Something like this would do the trick:

class StudentTimeTable {
  final String courseName;
  final String credit;
  final String hours;

  StudentTimeTable.fromMap(Map<String, String> map)
      : courseName = map['course_name'],
        credit = map['credit'],
        hours = map['hours'];

  @override
  String toString() =>
      'StudentTimeTable(courseName = $courseName, credit = $credit, hours = $hours)';
}

void main() {
  List<List<Map<String, String>>> input = [
    [
      {
        "course_name": "Estimation & Quantity Surveying",
        "credit": "4",
        "hours": "40",
      },
      {
        "course_name": "IDP - Industrial Design Project phase II",
        "credit": "4",
        "hours": "40",
      }
    ],
    [
      {
        "course_name": "Data Base Management System",
        "credit": "4",
        "hours": "40",
      },
      {
        "course_name": "Estimation & Quantity Surveying",
        "credit": "4",
        "hours": "40",
      },
    ],
  ];

  List<List<StudentTimeTable>> output = [
    ...input.map(
        (subList) => [...subList.map((map) => StudentTimeTable.fromMap(map))])
  ];

  output.forEach(print);
  // [StudentTimeTable(courseName = Estimation & Quantity Surveying, credit = 4, hours = 40), StudentTimeTable(courseName = IDP - Industrial Design Project phase II, credit = 4, hours = 40)]
  // [StudentTimeTable(courseName = Data Base Management System, credit = 4, hours = 40), StudentTimeTable(courseName = Estimation & Quantity Surveying, credit = 4, hours = 40)]
}

Explanation of what going on!

The solution makes use of "spread operator" which you can read more about here: https://dart.dev/guides/language/language-tour#spread-operator

In shot, it is a easy way to create a new list and take all elements in an iterable and put into the list.

So lets see what I do:

List<List<StudentTimeTable>> output = [...input.map((subList) => ...)]

Here we define a new list which are filled with the elements from input.map. The map method are used to take each element in the input and convert it to something else. In our case we want to convert each element in our input (which are also a List) from List<Map<String, String>> to List<StudentTimeTable>

We are then mapping each List<Map<String, String>> to the value from this:

[...subList.map((map) => StudentTimeTable.fromMap(map))]

Which returns a list filled with the elements from the iterator returned from subList.map. The purpose of this map is to convert Map<String, String> into StudentTimeTable.

This is done by calling our new constructor which takes a Map<String, String>:

  StudentTimeTable.fromMap(Map<String, String> map)
      : courseName = map['course_name'],
        credit = map['credit'],
        hours = map['hours'];

The same code could have been written something like this which is properly easier to read:

  final output = <List<StudentTimeTable>>[];

  for (final sublist in input) {
    final studentTimeTableSubList = <StudentTimeTable>[];

    for (final map in sublist) {
      studentTimeTableSubList.add(StudentTimeTable.fromMap(map));
    }

    output.add(studentTimeTableSubList);
  }

And a third way would be something like this which uses "collection for" from the same link about "spread operator":

  final output = [
    for (final sublist in input)
      [for (final map in sublist) StudentTimeTable.fromMap(map)]
  ];

Upvotes: 2

Related Questions