0% found this document useful (0 votes)
16 views4 pages

Flutter Day 2 Material

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Flutter Day 2 Material

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Basic Syntax and Data Types


Question: Write a Dart program that declares a list of integers and finds the sum of all elements in the
list.

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;

for (int number in numbers) {


sum += number;
}

// Alternatively, using reduce method


// int sum = numbers.reduce((a, b) => a + b);

print('Sum of the elements: $sum');


}

2. Functions and Control Flow

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:

List<int> findEvenNumbers(int max) {


List<int> evens = [];

for (int i = 1; i <= max; i++) {


if (i % 2 == 0) {
evens.add(i);
}
}

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;

Book(this.title, this.author, this.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:

Future<void> downloadFile() async {


print('Downloading file...');
await Future.delayed(Duration(seconds: 3));
print('Download complete');
}

void main() {
downloadFile();
print('Download started');
}

5. Collections and Exception Handling


Question: Write a Dart program that creates a map of student names and their scores. Add functionality
to retrieve a score by student name, handling cases where the name does not exist in the map.

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
};

int getScore(String name) {


if (studentScores.containsKey(name)) {
return studentScores[name]!;
} else {
throw Exception('Student not found');
}
}

try {
print('Score of Alice: ${getScore('Alice')}');
print('Score of Eve: ${getScore('Eve')}');
} catch (e) {
print(e);
}
}

You might also like