When we are developing mobile applications, making network calls is one of the most common tasks we do. Writing raw HTTP requests can quickly become repetitive and messy. This is where Chopper, a Flutter package, comes to the picture.
Chopper helps us manage HTTP requests in a clean, reusable, and efficient manner. In this article, In this article, we will see a step-by-step guide on how to use Chopper effectively and minimize our efforts.
What is Chopper?
Chopper is a powerful HTTP client generator for Dart and Flutter. It provides:
A clean and structured way to make API calls.
Automatic code generation for services.
Built-in features like interceptors, logging, and converters.
Instead of writing boilerplate HTTP code, you just define your API endpoints, and Chopper generates the implementation for you.
Installation
First, add the chopper to the dependencies and include build_runner and chopper_generator in the dev_dependencies section of your pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
chopper: ^8.3.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.7.1
chopper_generator: ^8.3.0
Then, run:
flutter pub get
Project Folder Structure

Creating the Network Manager
Let’s create a new file called ChopperNetworkManager.dart. This file will hold our service definition.
import 'package:chopper/chopper.dart';
part 'ChopperNetworkManager.chopper.dart';
@ChopperApi(baseUrl: "")
abstract class ChopperNetworkManager extends ChopperService {}
Now, generate the implementation by running the build command in your project root:
dart run build_runner build
This will generate a file named ChopperNetworkManager.chopper.dart with the actual implementation of your API calls.
Adding Singleton and API Setup
To make our service accessible throughout the app, we’ll use the singleton design pattern and configure Chopper with logging and JSON conversion.
Here, we defined one API call: getUsers, which fetches a list of users
import 'package:chopper/chopper.dart';
part 'ChopperNetworkManager.chopper.dart';
/// Chopper service defining API endpoints and client configuration.
@ChopperApi(baseUrl: "")
abstract class ChopperNetworkManager extends ChopperService {
static ChopperNetworkManager? manager;
/// Fetches users from the `/users` endpoint.
@GET(path: "users")
Future<Response> getUsers();
/// Creates a configured instance of [ChopperNetworkManager].
static ChopperNetworkManager create([ChopperClient? client]) {
client ??= ChopperClient(
baseUrl: Uri.parse("https://dummyjson.com"),
services: [_$ChopperNetworkManager()],
converter: JsonConverter(),
errorConverter: JsonConverter(),
interceptors: [HttpLoggingInterceptor()],
authenticator: null,
);
return _$ChopperNetworkManager(client);
}
/// Returns a lazily-initialized singleton instance.
static ChopperNetworkManager getInstance() {
manager ??= ChopperNetworkManager.create();
return manager!;
}
}


Join the conversation! Your thoughts help the community grow.