Paramclasses Academy - Blog

Tutorials Details

←
Post 7 of 40
→

How to Use Functions, Lists, Maps and Sets in Dart Programming | Param Classes

How to Use Functions, Lists, Maps and Sets in Dart Programming | Param Classes

November 2, 2026

❖ How to Use Functions, Lists, Maps and Sets in Dart Programming

✦ Welcome to Param Classes!

When learning Dart programming for Flutter app development, understanding functions and collections is very important. These concepts help you write reusable code, store multiple values, organize information, and manage application data.

Imagine you are developing a Student Management Application. You need functions to calculate student results, Lists to store student names, Maps to store student details, and Sets to maintain unique values.

In this tutorial, Param Classes will help you understand these concepts through simple explanations and practical Dart programming examples.

Let's start step by step!

✦ 1. What Is a Function in Dart?

A function is a block of code that performs a particular task. Instead of writing the same code multiple times, you can create a function and call it whenever required.

For example, you can create a function to display a welcome message.

Example 1: Simple Function

void main() {
  welcomeMessage();
}

void welcomeMessage() {
  print("Welcome to Param Classes!");
}

Output:

Welcome to Param Classes!

Explanation:

  • void means the function does not return a value.

  • welcomeMessage() is the function name.

  • The function body contains the code to execute.

  • welcomeMessage() inside main() calls the function.

✦ 2. Function with Parameters

Parameters allow you to pass information into a function.

For example, if you want to display different student names, you can use a parameter instead of creating separate functions.

Example 2: Function with One Parameter

void main() {
  studentName("Rahul");
  studentName("Amit");
  studentName("Priya");
}

void studentName(String name) {
  print("Student Name: $name");
}

Output:

Student Name: Rahul
Student Name: Amit
Student Name: Priya

Here, name is a parameter. Each time the function is called, a different value is passed to it.

✦ 3. Function with Return Value

A function can also return a result using the return keyword.

For example, you can create a function to calculate the sum of two numbers.

Example 3: Return Function

void main() {
  int result = addition(10, 20);

  print("Total: $result");
}

int addition(int a, int b) {
  return a + b;
}

Output:

Total: 30

Explanation:

  • int indicates that the function returns an integer.

  • addition() accepts two integer parameters.

  • return a + b sends the calculated result back.

  • The returned value is stored in result.

✦ 4. Arrow Functions in Dart

Dart supports a shorter syntax for functions that contain a single expression. It is called an arrow function.

Example 4: Arrow Function

void main() {
  print(square(5));
}

int square(int number) => number * number;

Output:

25

The arrow symbol => represents the expression returned by the function.

Arrow functions are useful for writing short and simple operations.

✦ 5. What Is a List in Dart?

A List is a collection that stores multiple values in an ordered sequence.

For example, you can use a List to store:

  • Student names.

  • Product names.

  • Mobile numbers.

  • Course names.

  • Marks.

Example 5: Create a List

void main() {
  List<String> students = [
    "Rahul",
    "Amit",
    "Priya",
    "Neha"
  ];

  print(students);
}

Output:

[Rahul, Amit, Priya, Neha]

Here, List<String> means that the List stores String values.

✦ 6. Access List Elements

You can access individual List elements using their index numbers.

Remember that List indexing starts from 0.

Example 6: Access List Items

void main() {
  List<String> students = [
    "Rahul",
    "Amit",
    "Priya"
  ];

  print(students[0]);
  print(students[1]);
  print(students[2]);
}

Output:

Rahul
Amit
Priya

The first element has index 0, the second has index 1, and the third has index 2.

✦ 7. Important List Methods in Dart

Dart provides several useful methods to manage List data.

A. add() — Add an Element

void main() {
  List<String> students = ["Rahul", "Amit"];

  students.add("Priya");

  print(students);
}

Output:

[Rahul, Amit, Priya]

B. remove() — Remove an Element

void main() {
  List<String> students = ["Rahul", "Amit", "Priya"];

  students.remove("Amit");

  print(students);
}

Output:

[Rahul, Priya]

C. length — Count Elements

void main() {
  List<String> students = ["Rahul", "Amit", "Priya"];

  print(students.length);
}

Output:

3

D. contains() — Check an Element

void main() {
  List<String> students = ["Rahul", "Amit", "Priya"];

  print(students.contains("Rahul"));
}

Output:

true

These methods are useful when creating applications that display, add, search, or remove records.

✦ 8. What Is a Map in Dart?

A Map is a collection that stores data in key-value pairs.

For example, a student's information can be stored using keys such as name, age, course, and mobile.

Example 7: Create a Map

void main() {
  Map<String, dynamic> student = {
    "name": "Rahul",
    "age": 21,
    "course": "Flutter",
    "fees": 15000
  };

  print(student);
}

Output:

{name: Rahul, age: 21, course: Flutter, fees: 15000}

Explanation:

  • Map creates a key-value collection.

  • String represents the key type.

  • dynamic allows values of different types.

  • Each key identifies its corresponding value.

✦ 9. Access and Update Map Values

You can access a Map value using its key.

Example 8: Access Map Values

void main() {
  Map<String, dynamic> student = {
    "name": "Rahul",
    "age": 21,
    "course": "Flutter"
  };

  print(student["name"]);
  print(student["age"]);
}

Output:

Rahul
21

Update an Existing Value:

void main() {
  Map<String, dynamic> student = {
    "name": "Rahul",
    "age": 21
  };

  student["age"] = 22;

  print(student);
}

Output:

{name: Rahul, age: 22}

Add a New Key:

student["city"] = "Raipur";

You can also remove a key using the remove() method.

student.remove("age");

Maps are especially useful when working with JSON data received from APIs.

✦ 10. What Is a Set in Dart?

A Set is a collection that stores unique values. It does not keep duplicate elements.

For example, if you have a list of student course registrations and want to maintain unique course names, a Set can be useful.

Example 9: Create a Set

void main() {
  Set<String> courses = {
    "Flutter",
    "PHP",
    "Python",
    "Flutter"
  };

  print(courses);
}

Output:

{Flutter, PHP, Python}

Notice that the duplicate Flutter value appears only once.

A Set does not provide index-based access like a List. Its iteration order should not be treated as a guaranteed sorting order.

✦ 11. Important Set Methods in Dart

A. add() — Add a Value

void main() {
  Set<String> courses = {"Flutter", "PHP"};

  courses.add("Python");

  print(courses);
}

B. remove() — Remove a Value

void main() {
  Set<String> courses = {"Flutter", "PHP", "Python"};

  courses.remove("PHP");

  print(courses);
}

C. contains() — Check a Value

void main() {
  Set<String> courses = {"Flutter", "PHP", "Python"};

  print(courses.contains("Flutter"));
}

Output:

true

D. length — Count Unique Values

void main() {
  Set<int> numbers = {10, 20, 30, 40};

  print(numbers.length);
}

Output:

4

✦ 12. Difference Between List, Map and Set

All three are collections in Dart, but they are used for different purposes.

Feature List Map Set
Stores Multiple values Key-value pairs Unique values
Duplicate values Allowed Keys must be unique Not allowed
Access By index By key No index-based access
Example Student names Student details Unique courses

Choose the collection based on how you want to store and access information.

✦ 13. Practical Project: Student Management Data

Now let's combine functions, Lists, and Maps in a simple student management example.

Complete Dart Program:

void main() {

  List<Map<String, dynamic>> students = [
    {
      "name": "Rahul",
      "age": 21,
      "course": "Flutter"
    },
    {
      "name": "Amit",
      "age": 22,
      "course": "PHP"
    },
    {
      "name": "Priya",
      "age": 20,
      "course": "Python"
    }
  ];

  displayStudents(students);

}

void displayStudents(List<Map<String, dynamic>> students) {

  for (var student in students) {

    print("Student Name: ${student["name"]}");
    print("Age: ${student["age"]}");
    print("Course: ${student["course"]}");
    print("----------------------");

  }

}

Output:

Student Name: Rahul
Age: 21
Course: Flutter
----------------------
Student Name: Amit
Age: 22
Course: PHP
----------------------
Student Name: Priya
Age: 20
Course: Python
----------------------

How Does This Program Work?

  1. A List stores multiple student records.

  2. Each student record is stored as a Map.

  3. The displayStudents() function receives the student List.

  4. A for-in loop visits each student record.

  5. The program displays the student's name, age, and course.

This is a useful example of combining Dart collections and functions in a real-world application.

✦ 14. Practice Tasks for Beginners

Try these exercises in DartPad:

Task 1: Create a function that prints your name and city.

Task 2: Create a function that accepts two numbers and returns their multiplication.

Task 3: Create a List containing five student names and display all names.

Task 4: Add a new product to a List and remove an existing product.

Task 5: Create a Map containing employee name, salary, and department.

Task 6: Update an employee's salary using the Map key.

Task 7: Create a Set containing five numbers, including one duplicate, and display the unique values.

Task 8: Create a function that accepts a List of numbers and returns their total.

Practise these examples regularly to improve your programming skills.

✦ 15. Why Are Functions and Collections Important in Flutter?

Functions and collections are used throughout Flutter application development.

For example:

  • Functions help organize application logic.

  • Lists help display multiple products or student records.

  • Maps help organize structured information.

  • Sets help maintain unique values.

  • Functions can process API responses and calculate results.

When you build a Flutter application, you will frequently work with Lists and Maps, especially when displaying data received from a backend API.

At Param Classes, we recommend practising these concepts before moving towards Flutter widgets, API integration, and database-connected applications.

✦ Conclusion

In this tutorial, we learned how to create functions, pass parameters, return values, and use arrow functions in Dart.

We also explored Lists, Maps, and Sets with practical examples and important collection methods.

Finally, we created a simple Student Management Data program by combining functions, Lists, Maps, and loops.

Remember, the best way to learn programming is to write code yourself. Practise every example, change the values, and try the exercises.

Param Classes helps beginners build strong programming foundations through practical tutorials and step-by-step learning.

Learn Today, Grow Tomorrow — Param Classes!

←
Post 7 of 40
→