tezvyn:

Make a GET request with http and parse JSON in Dart

AI-drafted, machine-checkedSource: dart.devbeginner

Practical Dart async networking with package:http and dart:convert. Import http, await get(Uri.parse(url)), verify statusCode is 200, then jsonDecode(response.body) as List<Map<String, dynamic>>. Skipping status checks or decoding without a cast.

WHAT THIS TESTS:

This question evaluates whether you understand the end-to-end flow of a typed REST call in Dart. The interviewer wants to see that you know how to use package:http for transport, dart:convert for serialization, and Dart's type system to safely cast unstructured JSON into a concrete collection shape. At the senior level, they also care about error boundaries and async hygiene.

A GOOD ANSWER COVERS:

A good answer hits five things in order. First, declare the dependency on package:http and import both package:http/http.dart and dart:convert. Second, construct a Uri object with Uri.parse rather than passing a raw string to the client. Third, await the GET call and immediately validate response.statusCode is 200 before touching the body. Fourth, decode the payload with jsonDecode and cast it explicitly to List<Map<String, dynamic>> so the compiler knows the structure. Fifth, mention that production code should wrap the network and decode steps in a try-catch block to handle SocketException or FormatException.

COMMON WRONG ANSWERS:

Red flags include calling http.get with a plain String instead of a Uri, skipping the status code check and blindly decoding the body, using dynamic as the return type instead of casting to List<Map<String, dynamic>>, and forgetting that http.get returns a Future so the function must be async. Another subtle mistake is calling jsonDecode and then mapping over the result without first casting, which leaves you with a List<dynamic> and pushes runtime errors downstream.

LIKELY FOLLOW-UPS:

The interviewer may ask how you would model this with strongly typed classes instead of a raw Map, which leads to fromJson constructors and json_serializable. They might also ask how to handle non-200 status codes with specific exceptions, how to add headers or query parameters, or how to cancel the request using a cancellation token pattern. You should also be ready to discuss timeouts and retry logic.

ONE CONCRETE EXAMPLE:

Imagine fetching a list of users from https://jsonplaceholder.typicode.com/users. You write final response = await http.get(Uri.parse(url)). Then you check if response.statusCode == 200. If true, you decode with final data = jsonDecode(response.body) as List<Map<String, dynamic>>. Each map in that list contains keys like id, name, and email. If the server returns a 500 or the payload is a single object instead of a list, the status check or the cast will fail fast and you can surface a meaningful error to the caller.

Read the original → dart.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.