Challenge - 5 Problems
Dio HTTP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Handling Dio request cancellation
You want to cancel an ongoing Dio HTTP request when the user navigates away from the screen. Which code snippet correctly cancels the request?
Flutter
final dio = Dio(); final cancelToken = CancelToken(); // Start request final response = await dio.get('https://example.com/data', cancelToken: cancelToken); // Later, cancel the request cancelToken.cancel('Request cancelled by user');
Attempts:
2 left
💡 Hint
Think about how Dio supports cancellation tokens to stop requests cleanly.
✗ Incorrect
Dio uses CancelToken to cancel requests. Calling cancelToken.cancel() stops the request and throws a DioError with type Cancel, which you can catch to handle cancellation gracefully.
🧠 Conceptual
intermediate1:30remaining
Understanding Dio interceptors order
Dio allows adding interceptors for requests and responses. What is the correct order of execution for multiple interceptors added to Dio?
Attempts:
2 left
💡 Hint
Think about how requests flow forward and responses flow backward.
✗ Incorrect
Dio executes request interceptors in the order they were added because requests flow forward. Response interceptors run in reverse order because responses flow backward through the chain.
📝 Syntax
advanced1:30remaining
Correct syntax for adding a custom header with Dio
Which option correctly adds a custom header 'Authorization: Bearer token123' to all Dio requests?
Flutter
final dio = Dio();Attempts:
2 left
💡 Hint
Check the Dio documentation for setting headers globally.
✗ Incorrect
Dio stores headers inside dio.options.headers. You can add or modify headers by assigning to this map.
❓ lifecycle
advanced2:00remaining
Handling Dio error types in Flutter
You want to handle different Dio error types separately in your Flutter app. Which code snippet correctly distinguishes a timeout error from a cancellation error?
Flutter
try { final response = await dio.get('https://example.com'); } on DioError catch (e) { if (e.type == DioErrorType.connectTimeout) { // Handle timeout } else if (e.type == DioErrorType.cancel) { // Handle cancellation } }
Attempts:
2 left
💡 Hint
DioError has a type property that indicates the error category.
✗ Incorrect
DioError.type is an enum that tells you the kind of error, such as connectTimeout or cancel. Checking this property is the correct way to handle different error cases.
🔧 Debug
expert2:30remaining
Diagnosing unexpected Dio response data
You receive a Dio response, but the data is unexpectedly null even though the server returns JSON. What is the most likely cause?
Flutter
final response = await dio.get('https://example.com/api'); print(response.data);
Attempts:
2 left
💡 Hint
Check the server response headers for content type.
✗ Incorrect
Dio automatically parses JSON if the server sends Content-Type: application/json. If this header is missing or wrong, Dio treats the response as plain text and data may be null or unexpected.