http_annotations 0.0.8 icon indicating copy to clipboard operation
http_annotations: ^0.0.8 copied to clipboard

http annotations for code generation for package http

Provides annotations for http code gen

Add in your pubspec.yaml

   dependencies:
      http_annotations:
   dev_depedencies:
      // package that processes the http_annotations for code generation
      http_gen:
      build_runner:

Generate the files dart run build_runner watch --delete-conflicting-outputs

Common imports on top of the file

   import 'package:http_annotations/http_annotations.dart';
   import 'package:http/http.dart';
   import 'dart:convert';
   import 'dart:async' show compute; // optional, if you use useFlutterCompute

Excluded generated files from analyzer in analysis_options.yaml

   analyzer:
      exclude:
         - lib/**/*.http.dart
  1. Add the generated file as a part of this file and annotate your class with @HttpApi()

    part 'file_name.http.dart';
    
    @HttpApi(
       'http://localhost:3000',
       useFlutterCompute: true, // is you want to use compute (defaults to false)
    )
    abstract class MyApi {
      // create a factory redirect (this is required)
      factory MyApi() = _$MyApi;
    
      // optional close method to dispose resources
      void close();
    }
    
  2. Annotate methods with appropirate HTTP Method and path

    @Route.get('/api/v0/todos')
    Future<List<Todo>> getTodos();
    

    Parameter replacement support

    @Route.get('/api/v0/todo/{id}')
    Future<Todo> getTodo(int id);
    
  3. Annotate methods with headers

    @Route.get('/api/v0/todos')
    @Header.contentTypeJson()
    @Header('cache', 'never')
    Future<List<Todo>> getTodos();
    
  4. Annotate methods with HTTP status codes that have body

    By default only status code 200 is supported Example, if you want to add for status codes 400 and 401

    @Route.get('/api/v0/user/login')
    @StatusCodesWithBody([200, 400, 401])
    Future<LoginResponse> login();
    
  5. Annotate a parameter with the body of the request (post, put, patch, delete)

    @Route.post('/api/v0/todo')
    Future<CreateTodoResponse>(@Body() Map<String, dynamic> json);
    
  6. Annotate a parameter with query parameter

    @Route.get('/api/v0/todos')
    Future<List<Todo>>(
      @QueryParam() int page,
      @QueryParam('named_limit') int limit,
    );
    
2
likes
130
pub points
0%
popularity

Publisher

unverified uploader

http annotations for code generation for package http

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

http

More

Packages that depend on http_annotations