Getting started
Last updated
Was this helpful?
Last updated
Was this helpful?
Due to limitations to Dart on Flutter and the Web browser, Chopper doesn't use reflection but code generation with the help of the and packages from the Dart Team.
Add the chopper
and the chopper_generator
packages to your project dependencies.
Run pub get
to start using Chopper in your project.
To define a client, use the @ChopperApi
annotation on an abstract class that extends the ChopperService
class.
The @ChopperApi
annotation takes one optional parameter - the baseUrl
- that will prefix all the request's URLs defined in the class.
Use one of the following annotations on abstract methods of a service class to define requests:
@Get
@Post
@Put
@Patch
@Delete
@Head
Request methods must return with values of the type Future<Response>
, Future<Response<SomeType>>
or Future<SomeType>
. The Response
class is a wrapper around the HTTP response that contains the response body, the status code and the error (if any) of the request. This class can be omitted if only the response body is needed. When omitting the Response
class, the request will throw an exception if the response status code is not in the range of < 200
to > 300
.
To define a GET
request to the endpoint /todos
in the service class above, add one of the following method declarations to the class:
or
or
Handling I/O and other exceptions should be done by surrounding requests with try-catch
blocks.
There's an exception from this behavior described in the section of the documentation.
URL manipulation with dynamic path, and query parameters is also supported. To learn more about URL manipulation with Chopper, have a look at the section of the documentation.
After defining one or more ChopperService
s, you need to bind instances of them to a ChopperClient
. The ChopperClient
provides the base URL for every service and it is also responsible for applying and on the requests it handles.