Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using data types like int or String instead of Exception.
Not using a catch block to handle errors.
✗ Incorrect
The catch block catches exceptions of type Exception during network calls.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The status code 200 means the request was successful, so we check if it equals 200.
3fill in blank
hardFix 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'
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.
✗ Incorrect
Using e.toString() converts the error object to a readable string message.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Not adding a delay before retrying.
✗ Incorrect
We check if status code equals 500 and wait 3 seconds before retrying.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using success message for error codes.
Mixing up error messages for status codes.
✗ Incorrect
These are standard HTTP error messages for the given status codes.