3 Best Http Clients for Flutter

Developers often choose a HTTP client when they need to send and get data from APIs. Flutter is new but it already gets supported with good Http clients.

Table of Contents

http

http contains a variety of high-level functions and classes that make it easy to send and get a HTTP request and response. It supports multi-platform, including mobile, desktop, and web.

import 'package:http/http.dart' as http;

var response = await http.get('https://jsonplaceholder.typicode.com/posts/1');
print(response);

dio

dio is a powerful Http client for Dart. It supports FormData, Request Cancellation, Interceptors, Global configuration, File downloading, Timeout, and more.

import 'package:dio/dio.dart';

void request() async {
  try {
    Response response = await Dio().get("https://jsonplaceholder.typicode.com/posts/");
    print(response);
  } catch (e) {
    print(e);
  }
}

There are many packages which make dio better.

PluginsDescription
dio_cookie_managerA cookie manager for Dio
dio_http2_adapterA Dio HttpClientAdapter which support Http/2.0
dio_flutter_transformerA Dio transformer especially for flutter, by which the json decoding will be in background with compute function.
dio_http_cacheA cache library for Dio, like Rxcache in Android. dio-http-cache uses sqflite as disk cache, and LRU strategy as memory cache.

retrofit

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

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

part 'example.g.dart';

@RestApi(baseUrl: "https://jsonplaceholder.typicode.com")
abstract class RestClient {
  factory RestClient(Dio dio, {String baseUrl}) = _RestClient;

  @GET("/posts")
  Future<List<Post>> getPosts();
}

@JsonSerializable()
class Post {
  String userId;
  String id;  
  String title;
  String body;

  Post({this.userId, this.id, this.title, this.body});

  factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json);
  Map<String, dynamic> toJson() => _$PostToJson(this);
}

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close