0
0
Fluttermobile~10 mins

Error handling for network calls in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to catch exceptions during a network call.

Flutter
try {
  final response = await http.get(Uri.parse('https://api.example.com/data'));
  print(response.body);
} catch ([1]) {
  print('Error occurred');
}
Drag options to blanks, or click blank then click option'
AException
Bint
CString
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using data types like int or String instead of Exception.
Not using a catch block to handle errors.
2fill in blank
medium

Complete the code to check if the HTTP response status code indicates success.

Flutter
if (response.statusCode [1] 200) {
  print('Success');
} else {
  print('Failed');
}
Drag options to blanks, or click blank then click option'
A==
B>=
C<=
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than or less than operators instead of equality.
Checking for status codes other than 200 for success.
3fill in blank
hard

Fix the error in the catch block to print the error message.

Flutter
try {
  final response = await http.get(Uri.parse('https://api.example.com/data'));
} catch (e) {
  print([1]);
}
Drag options to blanks, or click blank then click option'
Aerror
Be.toString()
Ce.message
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to print undefined variables like 'error' or 'exception'.
Using 'e.message' which may not exist on all exceptions.
4fill in blank
hard

Fill both blanks to retry the network call only if the status code is 500.

Flutter
if (response.statusCode [1] 500) {
  await Future.delayed(Duration(seconds: [2]));
  // retry logic here
}
Drag options to blanks, or click blank then click option'
A==
B3
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Not adding a delay before retrying.
5fill in blank
hard

Fill all three blanks to create a map of error messages for different status codes.

Flutter
final errorMessages = {
  400: '[1]',
  401: '[2]',
  404: '[3]'
};
Drag options to blanks, or click blank then click option'
ABad Request
BUnauthorized
CNot Found
DSuccess
Attempts:
3 left
💡 Hint
Common Mistakes
Using success message for error codes.
Mixing up error messages for status codes.