One of the biggest features of Flutter, which was released in version 1.9 a week ago, is that It incorporates the Flutter Web into the main branch. Today, Dio also officially released 3.0.0, which features support for Flutter Web and Http/2.0.

Note: Dio 3.0 needs to support Flutter Web, which requires major code refactoring, so it is not fully backward compatible. Users of Version 2.1 can refer to the 3.0 Upgrade Guide.

Support the Flutter Web

Developers simply need to upgrade Flutter to 1.9 or newer dev and Dio to the latest 3.x to support Flutter Web.

Http / 2.0 support

Http/2.0 has many features such as link reuse, header compression, binary transport, server push and so on. After Dio 3.0, a dio_http2_Adapter plug-in (HttpClientAdapter) is officially available to support Http/2.0. Here’s the official example:

import 'package:dio/dio.dart';
import 'package:dio_http2_adapter/dio_http2_adapter.dart';

main() async {
  vardio = Dio() .. options.baseUrl ="https://google.com". interceptors.add(LogInterceptor()) .. httpClientAdapter = Http2Adapter( ConnectionManager(idleTimeout:10000)); Response<String> response;
  response = await dio.get("/? xx=6");
  print(response.data);
}
Copy the code

As you can see, you only need to configure the Http2Adapter. It is worth noting that Http2Adapter requires a ConnectionManager parameter. ConnectionManager is responsible for managing links and is the implementation carrier of link reuse strategies in Http/2.0. The default ConnectionManager policy is to share a Socket connection for requests under the same domain name. When the request completes, the connection will remain on hold for 15 seconds by default. Developers can use idleTimeout to define the hold time. Developers provide their own custom link reuse strategy with ConnectionManager.

In addition, redirects are handled internally by Http2Adapter by default, which can be verified by the following code:

response.redirects.forEach((e){
  print("redirect: ${e.statusCode} ${e.location}");
});
Copy the code

Output:

redirect: 301 https://www.google.com/?xx=6 redirect: 302 https://www.google.com.hk/url?sa=p&hl=zh-CN&pref=hkredirect&pval=yes&q=https://www.google.com.hk/%3Fxx%3D6&ust=156881011 0125304&usg=AOvVaw0YbFhKFoslI0LPOPFcekGy redirect: 302 https://www.google.com.hk/?xx=6Copy the code

You can see that we were redirected three times when requesting Google in mainland China!

Other updates

  • FormData supports nesting.
  • Delete theUploadFileInfoClass, introducedMultipartFileClass;MultipartFileClass supports uploading header blocks not only from files, but also from streams, Byte arrays, and strings.
  • CookieManager is separated into separate packages; This is because cookies do not need to be manually managed in Flutter Web (the browser automatically manages them), so it makes more sense to extract them as a separate plug-in and introduce them on demand.
  • After the request is cancelled, the cancellation Error can be queued up by the interceptor (cancelled exceptions in 2.1 are thrown directly to the user).
  • Code optimization: API standardization, semantic; The core code has been completely refactored.

Please refer to: github.com/flutterchin… .