What function converts JSON string to Map?
jsonDecode() from dart:convert parses JSON strings into Dart objects.
Make API calls with the http package
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Most Flutter apps need to fetch data from a server or send data to one. The http package is the standard way to make network requests in Dart. It supports GET, POST, PUT, DELETE, and other HTTP methods. Every request returns a Response object that contains the status code, headers, and body. You check the status code to determine if the request succeeded, then parse the body to extract useful data.
1# Add to pubspec.yaml2dependencies:3http: ^1.1.0
1import 'dart:convert';2import 'package:http/http.dart' as http;34// GET request5Future<User> fetchUser(int id) async {6final response = await http.get(7Uri.parse('https://api.example.com/users/$id'),8headers: {'Authorization': 'Bearer token'},9);1011if (response.statusCode == 200) {12final json = jsonDecode(response.body);13return User.fromJson(json);14} else {15throw Exception('Failed to load user');16}17}1819// POST request20Future<User> createUser(String name, String email) async {21final response = await http.post(22Uri.parse('https://api.example.com/users'),23headers: {'Content-Type': 'application/json'},24body: jsonEncode({25'name': name,26'email': email,27}),28);2930if (response.statusCode == 201) {31return User.fromJson(jsonDecode(response.body));32} else {33throw Exception('Failed to create user');34}35}
Raw JSON data arrives as a Map of strings and dynamic values, which is error-prone to work with directly. A model class gives you type safety, autocomplete in your IDE, and a single place to define how your data is structured. Every model needs a fromJson factory constructor to parse incoming data and a toJson method to send data back to the server.
1class User {2final int id;3final String name;4final String email;56User({required this.id, required this.name, required this.email});78// From JSON9factory User.fromJson(Map<String, dynamic> json) {10return User(11id: json['id'],12name: json['name'],13email: json['email'],14);15}1617// To JSON18Map<String, dynamic> toJson() {19return {20'id': id,21'name': name,22'email': email,23};24}25}
Separate responsibilities clearly:
This separation prevents network logic from leaking into widgets and simplifies testing.
Make API calls with the http package. Networking lessons teach your app how to request, store, and display remote data safely.
Connect each concept to real app features you can build.
Always model loading, success, empty, and error states before hooking the UI.
Most production bugs happen at data boundaries: slow networks, bad payloads, and error states.
jsonDecode() from dart:convert parses JSON strings into Dart objects.
201 Created indicates the request succeeded and a new resource was created.
jsonEncode converts a Dart Map or List to a JSON string for sending in HTTP request bodies.
fromJson factory constructor parses a Map<String, dynamic> (from JSON) into a typed Dart object.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue