On this page24 sections
- Prerequisites
- Connection With Previous Lectures
- Learning Objectives
- 1. What Is a List?
- 2. Creating a List
- 3. Every List Has a Type
- 4. Writing the List Type Yourself
- 5. The Element Type Is Checked
- 6. How Many Elements? length
- 7. Empty Lists
- 8. isEmpty and isNotEmpty
- 9. Looping Over a List
- 10. Mixed Types — and Why to Avoid Them
- 11. Passing a List to a Function
- 12. A Real-World Example
- Code Examples
- Common Beginner Mistakes
- Best Practices
- Mini Challenge
- Practice Questions
- Practice Answers
- Quick Revision
- Key Takeaways
- What's Next?
Lecture 39 — Introduction to Lists
| Category | 7 — Lists |
| Lecture | 039 of 140 |
| Estimated video length | 9 min |
| Target | Dart 3.12.2 |
| Level | Beginner |
One variable holds one value, so thirty students would need thirty variables. A list keeps many values, in order, under one name.
Contents
Prerequisites · Connection · Objectives · Lesson · Code Examples · Common Mistakes · Best Practices · Mini Challenge · Practice Questions · Answers · Takeaways
Every error message in this lecture is real. Each one was produced by running the code shown on Dart 3.12.2, so you will recognize it when you meet it yourself.
Prerequisites
Students should already understand, from Lectures 004–038:
- variables,
var, and explicit types (Lectures 004–005) dynamicandObject(Lecture 011)ifstatements and+=(Lectures 017, 022)for-inloops over values in square brackets (Lecture 027)- functions with parameters and return values (Lecture 031)
Connection With Previous Lectures
Here is Ali's report with the tools from Lecture 004:
void main() {
int math = 80;
int science = 75;
int english = 85;
print(math + science + english);
}240Three subjects, three variables. Add a fourth subject and you need a fourth variable and a
changed total line. In Lecture 027 we looped over [85, 92, 78] without asking what that
bracketed group really is. Today we answer that: it is a list, and it has a type, a size,
and rules of its own.
Learning Objectives
By the end of this lecture you will be able to:
- Understand what a list is and why programs need one
- Create lists of
int,double,String, andboolvalues - Read and write list types such as
List<int>andList<String> - Create an empty list that still has a proper type
- Use
length,isEmpty, andisNotEmpty - Loop over a list with
for-into total, count, and print its values - Pass a list to a function
- Explain why lists of mixed types should be avoided
1. What Is a List?
A list is an ordered group of values stored in one variable.
Analogy — think of a shopping list on paper. It has one title at the top, and below it the items, one after another, in the order you wrote them.
void main() {
var marks = [80, 75, 85];
print(marks);
}[80, 75, 85]The values inside a list are called its elements. marks holds three elements.
2. Creating a List
Write the elements inside square brackets, separated by commas. The order you write is the order Dart keeps, and duplicates are allowed:
void main() {
var temperatures = [21.5, 25.0, 21.5, 19.0];
print(temperatures);
}[21.5, 25.0, 21.5, 19.0]print shows the whole list, brackets included. A list written straight into the code like
this is called a list literal.
3. Every List Has a Type
A list knows what kind of elements it holds. runtimeType (Lecture 010) shows it:
void main() {
var marks = [80, 75, 85];
var names = ['Ali', 'Sara', 'Zayan'];
print(marks.runtimeType);
print(names.runtimeType);
}List<int>
List<String>Read List<int> as "a list of int". The type inside the angle brackets < > is the
element type. This feature is called generics — we will study it properly in
Lecture 072 — Generics Fundamentals. For now, List<something> is all you need.
4. Writing the List Type Yourself
var lets Dart infer the type, just as in Lecture 005. You can also write it explicitly:
void main() {
List<int> marks = [80, 75, 85];
List<String> names = ['Ali', 'Sara'];
List<double> prices = [4.5, 12];
print(marks);
print(names);
print(prices);
}[80, 75, 85]
[Ali, Sara]
[4.5, 12.0]Like a double variable (Lecture 004), a List<double> stores the whole number 12 as
12.0.
5. The Element Type Is Checked
Once the type says int, every element must be an int:
void main() {
// List<int> marks = [85, 'ninety', 78]; // ✗
}Error: A value of type 'String' can't be assigned to a variable of type 'int'.
List<int> marks = [85, 'ninety', 78];
^Dart points at the exact element that doesn't belong — before the program even runs.
6. How Many Elements? length
length gives the number of elements — the same property you used on strings in
Lecture 008:
void main() {
var names = ['Ali', 'Sara', 'Zayan'];
print(names.length);
}3length is a property, not a function: no () after it.
7. Empty Lists
A list can start with no elements. Because [] alone gives Dart nothing to infer from,
state the element type, either on the variable or in front of the brackets:
void main() {
List<String> guests = [];
var scores = <int>[];
print(guests);
print(scores.length);
print(scores.runtimeType);
}[]
0
List<int>Both lines create an empty list that still knows its element type. Filling it comes in Lecture 041 — Adding & Removing Elements.
8. isEmpty and isNotEmpty
These two bool properties answer "is there anything in here?":
void main() {
List<String> guests = [];
var names = ['Ali', 'Sara'];
print(guests.isEmpty);
print(names.isEmpty);
print(names.isNotEmpty);
}true
false
trueguests.isEmpty reads better than guests.length == 0, and means the same.
9. Looping Over a List
The for-in loop from Lecture 027 visits each element in order. Combine it with += to
build a total:
void main() {
List<int> marks = [80, 75, 85];
var total = 0;
for (final mark in marks) {
total += mark;
}
print('Total: $total');
print('Subjects: ${marks.length}');
}Total: 240
Subjects: 3Add a fourth mark to the list and the total updates — no new variable, no edited line.
10. Mixed Types — and Why to Avoid Them
Dart allows a list with different kinds of values. It infers the closest type they share:
void main() {
var mixed = [85, 'Ali', true];
print(mixed.runtimeType);
}List<Object>Object (Lecture 011) fits every value, so you can only do what works for every value.
Math is not one of those things:
void main() {
var mixed = [85, 'Ali', true];
for (final item in mixed) {
// print(item * 2); // ✗
}
}Error: The operator '*' isn't defined for the type 'Object'.Keep one kind of value per list. Related values of different kinds — a name with its mark — belong in a record or a class, which come later in the course.
11. Passing a List to a Function
A list type works as a parameter type, like any other (Lecture 031):
int sumOf(List<int> numbers) {
var total = 0;
for (final number in numbers) {
total += number;
}
return total;
}
void main() {
print(sumOf([80, 75, 85]));
print(sumOf([]));
}240
0The parameter type List<int> tells Dart what [] must be, so even the empty list is a
proper List<int>.
12. A Real-World Example
double average(List<double> values) {
var total = 0.0;
for (final value in values) {
total += value;
}
return total / values.length;
}
void main() {
List<double> temperatures = [21.5, 25.0, 19.0, 27.5];
if (temperatures.isEmpty) {
print('No readings yet');
} else {
print('Readings: ${temperatures.length}');
print('Average: ${average(temperatures)}');
}
}Readings: 4
Average: 23.25One typed list, a check that it isn't empty, a loop inside a function, and length — the
whole lecture in one program.
Code Examples
Example 1 — A List of Numbers
void main() {
var marks = [90, 85, 77];
print(marks);
}[90, 85, 77]Example 2 — A List of Strings
void main() {
List<String> names = ['Ali', 'Sara', 'Zayan'];
print(names);
print(names.length);
}[Ali, Sara, Zayan]
3Example 3 — A List of Booleans
void main() {
List<bool> attendance = [true, false, true, true];
print(attendance);
}[true, false, true, true]Example 4 — Seeing the Type
void main() {
var prices = [4.5, 12.0];
var words = ['tea', 'milk'];
print(prices.runtimeType);
print(words.runtimeType);
}List<double>
List<String>Example 5 — An Empty List
void main() {
var orders = <String>[];
print(orders);
print(orders.length);
print(orders.isEmpty);
}[]
0
trueExample 6 — Greeting Everyone
void main() {
var names = ['Ali', 'Sara', 'Zayan'];
for (final name in names) {
print('Hello, $name!');
}
}Hello, Ali!
Hello, Sara!
Hello, Zayan!Example 7 — Student Marks
void main() {
List<int> marks = [80, 75, 85, 90];
var total = 0;
for (final mark in marks) {
total += mark;
}
print('Total: $total');
print('Average: ${total / marks.length}');
}Total: 330
Average: 82.5How it works
- The loop adds each mark into
total, giving330. marks.lengthis4, so the average is330 / 4./always produces adouble, so the average prints82.5.
Example 8 — Shopping Total
void main() {
List<double> prices = [250, 120.5, 80];
var total = 0.0;
for (final price in prices) {
total += price;
}
print('Items: ${prices.length}');
print('Total: $total');
}Items: 3
Total: 450.5How it works
250and80are stored as250.0and80.0because the list isList<double>.totalstarts as0.0, so it is adoubleand can take each price.
Example 9 — Bank Deposits
void main() {
var balance = 1000;
List<int> deposits = [200, 50, 300];
for (final deposit in deposits) {
balance += deposit;
}
print('Deposits: ${deposits.length}');
print('Balance: $balance');
}Deposits: 3
Balance: 1550How it works
balancestarts at1000.- Each deposit is added:
1200,1250,1550.
Example 10 — Counting Hot Days
void main() {
List<double> temperatures = [21.5, 31.0, 28.0, 33.5, 19.0];
var hotDays = 0;
for (final temperature in temperatures) {
if (temperature > 30) {
hotDays++;
}
}
print('Hot days: $hotDays of ${temperatures.length}');
}Hot days: 2 of 5How it works
- The loop checks every temperature with
if. - Only
31.0and33.5are above30, sohotDaysends at2.
Common Beginner Mistakes
1. An element of the wrong type
void main() {
// List<int> prices = [4.5, 12]; // ✗
}Error: A value of type 'double' can't be assigned to a variable of type 'int'.Use List<double> for prices with decimals.
2. Empty brackets with no type
void main() {
var orders = [];
print(orders.runtimeType);
}List<dynamic>No error — which is the problem. dynamic switches type checking off (Lecture 011). Write
<String>[] or List<String> orders = [];.
3. Calling length like a function
void main() {
var marks = [85, 92, 78];
// print(marks.length()); // ✗
}Error: 'length' isn't a function or method and can't be invoked.Write marks.length, without brackets.
4. Round brackets instead of square brackets
void main() {
// List<int> scores = (85, 92); // ✗
}Error: A value of type '(int, int)' can't be assigned to a variable of type 'List<int>'.Round brackets make a record, a different thing (Lecture 076). Lists use [ ].
5. Mixing kinds of values
[85, 'Ali', true] compiles, but becomes a List<Object> you can't calculate with. Keep
one kind of value per list.
Best Practices
- Give lists plural names (
marks,names) and loop variables the singular (mark). - Keep one kind of value per list.
- Always give an empty list a type:
<int>[]orList<int> x = [];. - Use
varwhen the elements make the type obvious; writeList<…>when it states your intent better, such asList<double>for prices. - Prefer
isEmpty/isNotEmptyover comparinglengthwith0. - Use
finalfor the loop variable when you only read each element.
Mini Challenge
Store Sara's marks [70, 88, 92, 65] in a List<int>. Print how many marks there are, the
total, and how many marks are 80 or more.
Your program should print exactly:
Marks: 4
Total: 315
80 or more: 2Try it yourself before opening the solution.
Show solution
void main() {
List<int> marks = [70, 88, 92, 65];
var total = 0;
var high = 0;
for (final mark in marks) {
total += mark;
if (mark >= 80) {
high++;
}
}
print('Marks: ${marks.length}');
print('Total: $total');
print('80 or more: $high');
}Marks: 4
Total: 315
80 or more: 2One loop does both jobs: every mark joins the total, and only high marks bump the counter.
Practice Questions
Work through these in order — they run from beginner to application to challenge.
Beginner
- Create a list of the cities
'Lahore','Karachi','Quetta'and print it. - Create a
List<int>of10,20,30and print itslength. - Create an empty
List<double>namedreadings, then print itsisEmpty. - Print the
runtimeTypeof[true, false].
Application
- Print each name in
['Ali', 'Sara', 'Zayan']asStudent: <name>. - Total the prices
[120, 80, 45]and printTotal: 245. - Count how many numbers in
[5, 12, 8, 20, 3]are greater than7. - Write a function
int countOf(List<String> items)that returns the number of items. Call it with['tea', 'milk', 'bread']and print the result.
Challenge
- For the temperatures
[18.5, 22.0, 25.5], print the number of readings and the average. - Write a function
void report(List<int> marks)that printsNo marksfor an empty list, and otherwise printsTotal: <total>. Call it with[]and with[60, 70].
Practice Answers
Question 1 — Solution
void main() {
var cities = ['Lahore', 'Karachi', 'Quetta'];
print(cities);
}[Lahore, Karachi, Quetta]Question 2 — Solution
void main() {
List<int> numbers = [10, 20, 30];
print(numbers.length);
}3Question 3 — Solution
void main() {
List<double> readings = [];
print(readings.isEmpty);
}trueQuestion 4 — Solution
void main() {
print([true, false].runtimeType);
}List<bool>Question 5 — Solution
void main() {
var names = ['Ali', 'Sara', 'Zayan'];
for (final name in names) {
print('Student: $name');
}
}Student: Ali
Student: Sara
Student: ZayanQuestion 6 — Solution
void main() {
var prices = [120, 80, 45];
var total = 0;
for (final price in prices) {
total += price;
}
print('Total: $total');
}Total: 245Question 7 — Solution
void main() {
var numbers = [5, 12, 8, 20, 3];
var count = 0;
for (final number in numbers) {
if (number > 7) {
count++;
}
}
print(count);
}3Question 8 — Solution
int countOf(List<String> items) {
return items.length;
}
void main() {
print(countOf(['tea', 'milk', 'bread']));
}3Question 9 — Solution
void main() {
var temperatures = [18.5, 22.0, 25.5];
var total = 0.0;
for (final temperature in temperatures) {
total += temperature;
}
print('Readings: ${temperatures.length}');
print('Average: ${total / temperatures.length}');
}Readings: 3
Average: 22.0Question 10 — Solution
void report(List<int> marks) {
if (marks.isEmpty) {
print('No marks');
return;
}
var total = 0;
for (final mark in marks) {
total += mark;
}
print('Total: $total');
}
void main() {
report([]);
report([60, 70]);
}No marks
Total: 130The early return (Lecture 031) handles the empty case first.
Quick Revision
- What is a list?
- What are the values inside a list called?
- How do you read
List<String>aloud? - What does
var x = [];create, and why is it a problem? - What is the difference between
isEmptyandisNotEmpty? - Why does
[85, 'Ali', true]become aList<Object>?
Key Takeaways
- A list is an ordered group of values, called elements, under one name.
- Create one with square brackets:
[80, 75, 85]. Order is kept; duplicates are allowed. List<int>means "a list ofint"; the element type is checked on every element.- Give empty lists a type:
List<int> x = [];or<int>[]— plain[]isList<dynamic>. lengthcounts elements;isEmptyandisNotEmptyanswer "anything in here?"for-invisits every element in order — ideal for totals and counts.- A list type works as a function parameter, like any other type.
- Mixed lists become
List<Object>; keep one kind of value per list.
What's Next?
Lecture 040 — List Elements & Indexes. So far we have treated a list as a whole: printed it, counted it, looped over it. Next we reach inside — reading one element by its position, changing it, and avoiding the classic "index out of range" crash.