Flutter Networking Tutorial: Getting Started

Update notes: Karol Wrótniak updated this tutorial for Flutter 3.10.0. The original version was written by Sagar Suri.

In today’s world, smartphones are the main hub of many activities: entertainment, banking, photography and videography and shopping, among others. To do the many things that their users request, apps on your smartphone need internet access.

If you plan to create apps that get data from the internet, you need to know about networking: how to make network requests, handle responses and errors. Throughout this tutorial, you will learn how to do this by creating a Flutter app named Bookshelf, which shows a list of popular books. You can edit, add and delete a book from the list.

Here’s what you do:

  • Learn how to run a PLEASE APPLY server using water channel framework of your computer.
  • Familiarize yourself GET, BRING, POST and DELETE requests.
  • Learn how to use the yes HTTP client to make network requests.
  • Learn how to use the json_serializable package to generate and parse JSON.
  • Assess Renewal by connecting dio with json_serializable.
  • understand Future, async and await.
NOTES: This tutorial assumes you have prior knowledge of Dart and the Flutter framework. If you are not familiar with Flutter, please take a look Getting started with Flutter.

dash

Download the starter project by clicking on the Download Materials button above or below the tutorial.

This article uses Android Studio. But you can also use VS Code or your favorite IDE with Flutter on the command line.

Run the starter app by pressing play in Android Studio or using a keyboard shortcut Control-R on macOS or Shift-F10 on Windows or Linux. You will see this screen:
Main start screen of the bookshelf app

Notice that nothing but a is visible lorem ipsum placeholder. It will show a list of your favorite books after you complete the project.

Project Exploration

Once you’ve run the startup project, it’s time to look at the project structure:

├── api
│   ├── bin
│   │   └── main.dart
│   └── lib
│       ├── api.dart
│       ├── books_controller.dart
│       ├── bookstore.dart
│       └── channel.dart
└── lib
    ├── main.dart
    ├── model
    │   └── book.dart
    ├── network
    │   └── data_source.dart
    └── ui
        ├── add_or_update_book_screen.dart
        └── bookshelf_screen.dart

Here is the purpose of each directory:

  • maltreatment: Holds the logic of the server, you will not work with files in this folder.
  • model: Contains the book data model class.
  • network: Holds the app’s networking logic.
  • yes: Contains the UI screens for the app.

Definition of Important Terms

Take a moment to make sure you understand the terminology used in this tutorial.

Understanding Network Requests and Responses

In simple terms, when you use apps like WhatsApp or Twitter, they try to transfer some data from or to a server. The diagram below is a simple illustration of that flow:

Request a network response

The app you are using is clients. Therefore, a client creates a network REQUEST on a server, and it responds to a ANSWERS. There are different ways to transfer data this way. One of the most popular is through a PLEASE APPLY.

Understanding RESTful APIs

holiday stands for State Transfer Representation. It is an application program interface – API. It uses HTTP requests to get or send data between computers.

Communication between a client and a server usually happens through RESTful APIs, and that’s what you’ll be using in this tutorial.

Understanding End Points

An endpoint is an end-of-communication channel between the server and your app. Servers provide access to REST API endpoints through URLs. For example, if you have URLs and common prefixes: is a base URL. The variable suffix /users and /products is the final points.

NOTES:, there must be a slash separating the base URL and the endpoint. By convention, in Flutter, there is usually no trailing slash in the base URL. However, endpoints have leading slashes. The generated code under the hood combines a base URL with a path to create the full URL and then normalizes the result.
So the other way around (trailing slash in a base URL) or slash in two positions also works.

Understanding HTTP Methods

An HTTP method is an action you want to perform. There are many HTTP methods that you can use in the REST API. Here’s what they did:

  • GET: Downloads the specified resource. Requests using GET only retrieve data; they don’t change it.
  • DELETE: Delete the specified resource.
  • POST: Submits data to the specified resource. Usually creates new things on the server.
  • BRING: Replace the entire target resource with the uploaded one. It may create a new object without the target.
  • PATCH: Applies partial updates to the target resource.
  • head: Behave as GET but the body did not return. Useful for quick checks to see if something is on the server or how big it is.

There are many HTTP methods, but some are rarely used in app development. See the complete list in the MDN web docs. Note that the server does not need to implement all methods for all endpoints.

Notice the differences between PUT and POST. the BRING requests idempotent: No matter how many times you repeat this, the state of the backend should be the same. However, in the case of POST, if you send the same request multiple times, you can multiply the results. For example, do more things on the server.
Usually, a BRING requires a unique identifier as a parameter so that the backend logic can select the subject to modify.

Understanding HTTP Status Codes

Every HTTP response contains a metadata. The most important part of it is status code — three-digit decimal number:

HTTP status codes

It tells the client whether the request was successful or not. You probably remember 404 – Not found errors, where 404 is the status code. In general, status codes range from 100-599:

  • 2xx — from 200 to 299 — means success.
  • 4xx and 5xx means failure.
  • 1xx and 3xx available only at lower abstraction layers such as HTTP client internals. They are not used to progress forward.

NOTES: The HTTP request may fail due to network issues such as a dead internet connection. In such cases, there is no HTTP status because no response can be read. In this tutorial, you won’t deal directly with HTTP status codes. The libraries you use will do this for you. They throw Dart exceptions when there are unsuccessful status codes.

Leave a comment