Maps

1. Create a Student Object

In Dart, you can represent an object like a student using a Map with key-value pairs.

Example Code

void main() {
  // Single Student Object as a Map
  Map<String, dynamic> student = {
    "id": 1,
    "name": "John Doe",
    "age": 20,
    "grade": "A"
  };

  print("Student Object: $student");
}

Output:

Student Object: {id: 1, name: John Doe, age: 20, grade: A}

2. Manipulate the Map

A. Append a New Key-Value Pair

To add a new property to the Map, simply use the key and assign a value.

void main() {
  Map<String, dynamic> student = {
    "id": 1,
    "name": "John Doe",
    "age": 20,
    "grade": "A"
  };

  // Add a new key-value pair
  student["email"] = "johndoe@example.com";
  print("After Adding Email: $student");
}

Output:

After Adding Email: {id: 1, name: John Doe, age: 20, grade: A, email: johndoe@example.com}

B. Remove a Specific Key

Use the .remove() method to delete a key-value pair by its key.

void main() {
  Map<String, dynamic> student = {
    "id": 1,
    "name": "John Doe",
    "age": 20,
    "grade": "A"
  };

  // Remove the 'grade' key
  student.remove("grade");
  print("After Removing Grade: $student");
}

Output:

After Removing Grade: {id: 1, name: John Doe, age: 20}

C. Edit a Value

To update a value, access the key and assign it a new value.

void main() {
  Map<String, dynamic> student = {
    "id": 1,
    "name": "John Doe",
    "age": 20,
    "grade": "A"
  };

  // Update the 'age' value
  student["age"] = 21;
  print("After Updating Age: $student");
}

Output:

After Updating Age: {id: 1, name: John Doe, age: 21, grade: A}

D. Find a Value by Key

To retrieve a value, use the key directly.

void main() {
  Map<String, dynamic> student = {
    "id": 1,
    "name": "John Doe",
    "age": 20,
    "grade": "A"
  };

  // Find value by key
  print("Student Name: ${student['name']}");
  print("Student Age: ${student['age']}");
}

Output:

Student Name: John Doe
Student Age: 20

3. Full Example

Here’s the full code combining all the operations:

void main() {
  // Single Student Object
  Map<String, dynamic> student = {
    "id": 1,
    "name": "John Doe",
    "age": 20,
    "grade": "A"
  };

  print("Original Student Object: $student");

  // Append
  student["email"] = "johndoe@example.com";
  print("After Adding Email: $student");

  // Edit
  student["age"] = 21;
  print("After Updating Age: $student");

  // Find
  print("Student Name: ${student['name']}");
  print("Student Email: ${student['email']}");

  // Remove
  student.remove("grade");
  print("After Removing Grade: $student");
}

Output

Original Student Object: {id: 1, name: John Doe, age: 20, grade: A}
After Adding Email: {id: 1, name: John Doe, age: 20, grade: A, email: johndoe@example.com}
After Updating Age: {id: 1, name: John Doe, age: 21, grade: A, email: johndoe@example.com}
Student Name: John Doe
Student Email: johndoe@example.com
After Removing Grade: {id: 1, name: John Doe, age: 21, email: johndoe@example.com}

Operation

Method

Example

Append

map["key"] = value

student["email"] = "abc@mail";

Remove

map.remove("key")

student.remove("age");

Edit/Update

map["key"] = newValue

student["age"] = 22;

Find by Key

map["key"]

print(student["name"]);


Updated on