Flutter Day 2 Material
Flutter Day 2 Material
Hint:
Declare a list of integers.
Use a loop or the reduce method to calculate the sum of the elements.
Idea to Solve:
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
int sum = 0;
Question: Write a Dart function that takes an integer as input and returns a list of all even numbers up to
that integer.
Hint:
Define a function with an integer parameter.
Use a loop to iterate from 1 to the input integer.
Check if the number is even and add it to a list.
Idea to Solve:
return evens;
}
void main() {
int max = 10;
List<int> evens = findEvenNumbers(max);
print('Even numbers up to $max: $evens');
}
3. Object-Oriented Programming
Question: Create a class Book with properties title, author, and pages. Add a method details that prints
the book's details. Create a few instances and call the method.
Hint:
Define the class with the required properties and a constructor.
Define the details method.
Create instances and call the details method on them.
Idea to Solve:
class Book {
String title;
String author;
int pages;
void details() {
print('Title: $title, Author: $author, Pages: $pages');
}
}
void main() {
Book book1 = Book('1984', 'George Orwell', 328);
Book book2 = Book('To Kill a Mockingbird', 'Harper Lee', 281);
book1.details();
book2.details();
}
4. Asynchronous Programming
Question: Write an asynchronous Dart function that simulates downloading a file. The function should
wait for 3 seconds and then print "Download complete".
Hint:
Use the Future and await keywords.
Use Future.delayed to simulate the delay.
Idea to Solve:
void main() {
downloadFile();
print('Download started');
}
Hint:
Create a map of student names and scores.
Write a function to retrieve a score by name.
Use try-catch or null checks to handle missing names.
Idea to Solve:
void main() {
Map<String, int> studentScores = {
'Alice': 85,
'Bob': 92,
'Charlie': 78
};
try {
print('Score of Alice: ${getScore('Alice')}');
print('Score of Eve: ${getScore('Eve')}');
} catch (e) {
print(e);
}
}