retrofit generator is an dio client generator using source_gen and inspired by Chopper and Retrofit.

build-runner, chopper, dart, dio, http, retrofit, source-gen


Retrofit For Dart

CircleCI Build Status Build Status

retrofit.dart is a type conversion dio client generator using source_gen and inspired by Chopper and Retrofit.



Add the generator to your dev dependencies

  retrofit: any
  logger: any  #for logging purpose

  retrofit_generator: any
  build_runner: any

Define and Generate your API

import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';

part 'example.g.dart';

@RestApi(baseUrl: "")
abstract class RestClient {
  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  Future<List<Task>> getTasks();

class Task {
  String id;
  String name;
  String avatar;
  String createdAt;

  Task({,, this.avatar, this.createdAt});

  factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json);
  Map<String, dynamic> toJson() => _$TaskToJson(this);

then run the generator

# dart
pub run build_runner build

# flutter	
flutter pub run build_runner build

Use it

import 'package:logger/logger.dart';
import 'package:retrofit_example/example.dart';
import 'package:dio/dio.dart';

final logger = Logger();
void main(List<String> args) {
  final dio = Dio();   // Provide a dio instance
  dio.options.headers["Demo-Header"] = "demo header";   // config your dio headers globally
  final client = RestClient(dio);
  client.getTasks().then((it) => logger.i(it));


Type Conversion

Before you use the type conversion, please make sure that a factory Task.fromJson(Map<String, dynamic> json) must be provided for each model class. json_serializable is the recommanded to be used as the serialization tool.

  Future<List<Task>> getTasks();

class Task {
  String name;
  factory Task.fromJson(Map<String, dynamic> json) => _$TaskFromJson(json);

HTTP Methods

The HTTP methods in the below sample are supported.

  Future<Task> getTask(@Path("id") String id);

  Future<Task> updateTaskPart(
      @Path() String id, @Body() Map<String, dynamic> map);

  Future<Task> updateTask(@Path() String id, @Body() Task task);

  Future<void> deleteTask(@Path() String id);

  Future<Task> createTask(@Body() Task task);

  Future<void> createNewTaskFromFile(@Part() File file);

  Future<String> postUrlEncodedFormData(@Field() String hello);

Get orignal HTTP reponse

  Future<HttpResponse<Task>> getTask(@Path("id") String id)

  Future<HttpResponse<List<Task>>>> getTasks()

HTTP Header

  • Add a HTTP header from the parameter of the method

      Future<Task> getTasks(@Header("Content-Type") String contentType );
  • Add staitc HTTP headers

    	@Headers(<String, dynamic>{
    		"Content-Type" : "application/json",
    		"Custom-Header" : "Your header"
      Future<Task> getTasks();

Error Handling

catchError(Object) should be used for capturing the exception and failed response. You can get the detailed response info from DioError.response.

 }).catchError((Object obj) {
    // non-200 error goes here.
    switch (obj.runtimeType) {
      case DioError:
        // Here's the sample to get the failed response error code and message
        final res = (obj as DioError).response;
        logger.e("Got error : ${res.statusCode} -> ${res.statusMessage}");