-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnull_safety.dart
More file actions
31 lines (28 loc) · 1 KB
/
null_safety.dart
File metadata and controls
31 lines (28 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
PRATICAL EXAMPLE
- Imagine a school where students take an exam.
- The exam is out of 20.
- At the end of the year, the school wants to print out the marks of every student
- However; not all students took the exam.
*/
class Student {
late final String
name; // the late final means we don't give a value to this variable at the time of declaration, but we can give it a value later.
late final int? marks; //the ? means we can give it a value , it can be null.
Student({required this.name, this.marks});
}
void main() {
List<Student> students = [
Student(name: 'Alice', marks: 15),
Student(name: 'sma3il', marks: 0),
Student(name: "lwahch"),
];
for (var student in students) {
print(student.name + "'s mark is : " + calculatepercentage(student.marks));
}
}
String calculatepercentage(int? marks) {
int the_total_marks = 20;
double percentage = (marks?.toDouble() ?? 0) * 100 / the_total_marks;
return marks == null ? "majash lexamen" : percentage.toStringAsFixed(0) + "%";
}