Group Group Group Group Group Group Group Group Group

Elegant Networking in Flutter with Chopper | raywenderlich.com

Learn how to easily create REST API calls and parse JSON data from the internet using the Flutter networking library Chopper.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/10099546-elegant-networking-in-flutter-with-chopper

Hey @kevindmoore, The Article is just awesome :raised_hands: can we expect POST functionality soon or could you help us how to POST Data.

A POST shouldn’t be too hard. You just need to have something like:
@Post(path: “cats”)
Future postData(@Body() String data);

Hey @kevindmoore , Thanks for this great article. Please I’d to know it the ModelConverter works for any Model. Thx

You would have to change it to work with your models

Dear Kevin, thank you for the article. It helped me a lot. There’s a question about using several @Get paths. For example I also want to get /upcoming movies/
@Get(path: ‘movie/upcoming’)
Future<Response> getUpcomingMovies();
As far as I understand I should create a converter for each @Get() because I use different data there from different classes.
If I create a separate UpcomingModelConverter() I can’t use it in static MovieService create() {

converter: because I already have here ModelConverter() for popular movies

}
Could you clarify how to make several requests and process them, please?

Great question. What I would do is in the ModelConverter class and in the decodeJson method, I would look at the json returned and see what type it is (by peeking into the string), then use a different class’s fromJSON method.
If that doesn’t work, you could create a separate service for each return type.

Hi kevindmoore,

Thank you for this tutorial. I have an important piece of info for this one, and I suggest you update the tutorial with it.

I tried to make a post request and followed how you did it. The problem was with Interceptors. You see, the chopper will first call model converters and then Interceptors. So if we add an auth token as we do with this example, it will completely overwrite headers.

This might not always be a good thing and can lead to 4 hours of debugging like in my case :smiley:
My server was only ready to accept Content-type application/json, and nothing else. I specified it in a model converter and then added auth token in Intercepter.

So I copied your code.
Request newRequest = request.copyWith(headers: {
AUTH_HEADER: APIKEY,
});
return newRequest;

It erased my Content-type application/json and left it only with auth token. And then the underlying http client just filled Content-type in with the plain text.

the way I fixed it was using another method chopper:
@override
FutureOr onRequest(Request request) async {
return applyHeader(request, AUTH_HEADER, APIKEY);
}

this only adds headers and doesn’t erase anything. The other way would be to specify the Content-type application/json here one more time.

For this tutorial, it doesn’t make a big difference because TMDB API will forgive it, but other situations might not be ok with it.

I suggest you change a tutorial with applyHeader in onRequest of Interceptor or add a comment there to watch out.

Thank you!

Wow, thanks so much for the detailed suggestions. I will take a look as I do something similar in the Flutter book.