Challenge your understanding of Flutter networking by answering questions focused on REST API integration, asynchronous requests, JSON parsing, and practical coding scenarios. This quiz covers key techniques, error handling, and best practices for efficient data management in Flutter applications.
Which HTTP method should you use to update an existing resource on a REST API in a Flutter app?
Explanation: PUT is used for updating an existing resource on a REST API. GET retrieves data, POST creates new resources, and DELETE removes resources. Using PUT ensures your update request is idempotent, while the other methods have different purposes or side effects in RESTful design.
How do you ensure that your Flutter app does not freeze when performing HTTP requests to a REST API?
Explanation: Using async-await with Future ensures that HTTP requests are performed asynchronously, preventing the app's UI from freezing. Placing requests in the build method can cause repeated fetching and performance issues. 'Print' statements are only for debugging, and running synchronously on the main thread will block the UI.
What is the correct way to convert a JSON string response from an API into a Dart Map object?
Explanation: The jsonDecode() function takes a JSON string and converts it into a Dart object such as a Map or List. encodeJson() and parseJson() are not valid functions, while toJson() is typically used for converting a Dart object to a JSON string, not the other way around.
When receiving an HTTP 404 status code while fetching data in Flutter, what does it indicate?
Explanation: A 404 status code means the requested resource does not exist on the server. A 200 code shows success, a 401 means unauthorized access, and the server being down might produce a 500 or connectivity error rather than a 404.
Which approach is commonly used to convert a Dart object into a JSON map for sending data to a REST API?
Explanation: Defining a toJson() method allows a Dart object to be converted to a Map suitable for JSON encoding. fromJson() is used for deserialization, not serialization. print() outputs text only, and static variables are unrelated to JSON mapping.
What is the recommended way in Flutter to handle exceptions that may occur during an HTTP request to a RESTful API?
Explanation: Using try-catch ensures that exceptions like network errors are caught and handled gracefully. Ignoring exceptions can crash the app, writing errors to a file directly is not standard error handling for HTTP requests, and running an infinite loop can freeze the app.
When making a POST request to a REST API in Flutter, where should you typically place the data payload?
Explanation: Data payloads for POST requests are placed in the request body. Putting data in the header or URL path is incorrect for sending structured data, and not sending the payload at all means nothing is transmitted to the server.
If a REST API returns a JSON array of objects, such as a list of users, what is the standard Dart type for the decoded JSON?
Explanation: Decoding a JSON array of objects gives a List of Map objects where each map holds string keys and dynamic values. A Mapu003CString, Objectu003E represents a single map, Listu003CStringu003E and Setu003CStringu003E only hold strings, which do not match object structure.
To ensure the server knows you are sending JSON data in a Flutter HTTP request, which header should you set?
Explanation: Content-Type: application/json explicitly tells the server that the request body contains JSON data. Accept-Language manages language preferences, Cache-Control deals with caching, and User-Agent identifies the client but does not specify content format.
How can you include query parameters (like search terms or filters) in a Flutter GET request URL?
Explanation: Query parameters are appended to the URL after a question mark for GET requests. Placing them in the body or headers is improper for GET, and global variables do not influence the actual HTTP request.
What pattern can you use in Flutter to prevent making the same REST API call every time a widget rebuilds?
Explanation: Fetching data in initState and storing it avoids repeated network calls on widget rebuilds. Calling in the build method or setState may trigger multiple requests, while constructors are not ideal for asynchronous logic.
When decoding a JSON response with nested objects in Flutter, what is the recommended strategy to map the data to Dart objects?
Explanation: Using separate Dart classes for nested structures and their own fromJson methods keeps code organized and manageable. Flattening loses structure, top-level parsing ignores inner data, and primitive types do not model complex objects.
Which Flutter widget is commonly used to implement pull-to-refresh functionality for updating data fetched from REST APIs?
Explanation: RefreshIndicator provides a pull-to-refresh UI and triggers a callback to re-fetch data. ListView.builder displays lists, Drawer is for navigation, and AppBar shows app titles and actions, not refresh functionality.
If your Flutter app receives a very large JSON response, what can you do to avoid performance issues during decoding?
Explanation: Decoding large JSON data off the main thread using an isolate prevents the UI from stalling. Increasing UI refresh rate or using only stateless widgets doesn't address main thread blocking, while encoding twice is unnecessary and wasteful.
What is a common way to include authentication tokens when making secure API calls from a Flutter app?
Explanation: The Authorization header is the standard location for API tokens. Placing tokens in URLs can expose them in logs, storing tokens in widgets is insecure, and omitting tokens prevents authentication.
If a Flutter app displays a timeout error when connecting to an API, what is a possible cause?
Explanation: A timeout usually indicates that the server is slow or unresponsive. Incorrect JSON format can cause parsing errors, duplicate headers might result in malformed requests, and null values in widgets trigger Flutter errors, not network timeouts.