From Flutter to Backend: How to Build and Ship Production REST APIs with Dart and Shelf

Iniciado por joomlamz, 01 de Junho de 2026, 22:00

Respostas: 0   |   Visualizações: 17

Tópico anterior - Tópico seguinte

0 Membros e 1 Visitante estão a ver este tópico.


                     From Flutter to Backend: How to Build and Ship Production REST APIs with Dart and Shelf
               




Tópico:
                     From Flutter to Backend: How to Build and Ship Production REST APIs with Dart and Shelf
               
Categoria: Tutoriais | FreeCodeCamp Premium
Idioma Principal: Português (Conteúdo de Tecnologia)

Conteúdo do Tutorial / Guia Passo a Passo:
-------------------------------------------------------------------------
As a Flutter engineer, you already know Dart. You understand async/await, you work with models and repositories, you think in clean architecture, and you have shipped real applications.

The gap between where you are and being able to build and deploy a production backend is smaller than you think.

The missing piece is not a new language. It's not a new paradigm. It's understanding how Dart behaves when there's no widget tree, no BuildContext, no Flutter framework – just a running process handling HTTP requests, talking to a database, and sending responses back to clients.

That's exactly what this article covers.

We're going to build a full User and Profile Management REST API from scratch using Dart and Shelf, connect it to a PostgreSQL database running in Docker, secure it with JWT authentication, and deploy it to Fly.io.

By the end, you'll have a working production-grade backend written entirely in Dart, the same language you already know.

This article is part of a series (of standalone articles) where we'll build the same project using three different frameworks. We'll use Shelf here, Serverpod in the next article, and Dart Frog in the one after that. This will let you directly compare how each framework approaches the same problem.

Table of Contents

• Prerequisites

• How Dart Works on the Server

• What is Shelf?

• Project Setup

• Creating the Project

• Project Structure

• Database Setup with Docker

• Environment Configuration

• Shelf Core Concepts

• Handlers

• Request and Response

• Router

• Pipeline and Middleware

• Connecting to PostgreSQL

• The Database Connection Manager

• Running Migrations

• Building the API

• The User Model

• The User Repository

• User Handlers

• The Profile Model

• The Profile Repository

• Profile Handlers

• Authentication

• Password Hashing

• JWT Token Generation and Validation

• Auth Handlers

• Auth Middleware

• Error Handling

• Wiring Everything Together

• Deployment

• Dockerfile

• Docker Compose for Local Production Testing

• Deploying to Fly.io

• Testing the API

• Conclusion

Prerequisites

Before starting, you should have:

• Comfortable familiarity with Dart and Flutter development

• Understanding of REST API concepts, endpoints, HTTP methods, status codes

• Docker Desktop installed and running

• A Fly.io account (free tier is sufficient, fly.io)

• The Fly CLI installed (brew install flyctl on macOS, or the official installer on Windows/Linux)

• A PostgreSQL client for inspecting the database, like TablePlus or DBeaver – both work well

How Dart Works on the Server

When you run a Flutter app, the Flutter framework is doing an enormous amount of work, managing the widget tree, handling the render pipeline, coordinating state, and responding to platform events. Your Dart code sits on top of all of that.

On the server, none of that exists. There's no widget tree. There's no framework managing a UI lifecycle. There's just a Dart process running, listening on a port, receiving HTTP requests, doing work, and sending responses.

Dart's standard library, dart:io, has everything needed to do this at the lowest level:

import 'dart:io';

void main() async {
final server = await HttpServer.bind('0.0.0.0', 8080);
print('Server running on port 8080');

await for (final request in server) {
request.response
..statusCode = 200
..write('Hello from Dart')
..close();
}
}

This is a

... [O tutorial continua no link abaixo] ...


Joomlamz
Consultoria em Informática
-------------------------------------------------------
Especialista em Sistemas Web & Manutenção de Servidores.
A desenvolver o novo AplPortal com suporte a PHP 8.
Precisa de ajuda profissional? Contacte-me.

Tags: