androidflutterflutter-http

In Android 12+, the REST API calls are very slow in flutter, In WiFi that takes 10 seconds whereas in mobile data it is less than a second


I am facing an issue with Flutter Android 12+ version. App-consuming REST API calls using the Flutter http plugin (Version: 0.13.5). All API calls are HTTPS service calls with the domain name. API calls take 10+ seconds to provide the response whereas the same API calls took only less than a second in mobile data networks. The same API calls I tried using Java Client and Postman, all responses took only less than a second to get a response.

During the troubleshooting, I tested the following scenarios using Wifi Network

  1. Instead of domain I executed the API service call using the IP address, which took only less than a second (SSL verification disabled in HTTP plugin)
  2. Tested API call using connection keep-alive: true, then the first call took 10+s after that subsequent calls took only less than a second
  3. One interesting fact is that I experienced a problem only in Android 12+, the Android 11 device worked on a WiFi network, and it took less than a second for a service call.

Now I understood Wifi based API calls always take 10 Seconds + actual service call time (eg 400 ms hence it would be like 10.400 seconds).

Is there any specific reason for this issue?

flutter doctor -v
[√] Flutter (Channel stable, 3.3.6, on Microsoft Windows [Version 10.0.22000.1219], locale en-US)
    • Flutter version 3.3.6 on channel stable at C:\Applns\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 6928314d50 (6 weeks ago), 2022-10-25 16:34:41 -0400
    • Engine revision 3ad69d7be3
    • Dart version 2.18.2
    • DevTools version 2.15.0

[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at C:\Users\Siju\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.4.1)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.4.33110.190
    • Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2021.2)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+7-b1504.28-7817840)

[√] Connected device (4 available)
    • 2201116SI (mobile) • b75ac32a6d34 • android-arm64  • Android 12 (API 31)
    • Windows (desktop)  • windows      • windows-x64    • Microsoft Windows [Version 10.0.22000.1219]
    • Chrome (web)       • chrome       • web-javascript • Google Chrome 107.0.5304.123
    • Edge (web)         • edge         • web-javascript • Microsoft Edge 107.0.1418.62

[√] HTTP Host Availability
    • All required HTTP hosts are available

Solution

  • This issue is due to the IPv4/IPv6 DNS resolution in the flutter HTTP client. During the HTTP service call, the dart client tries the DNS resolution using an IPv6 address instead of IPv4 where DNS servers don't support the IPV6 version hence it waits for 10s later it fallback to IPv4 and establishes the connection. Many flutter developers reported this issue in the Flutter git repo.

    All these issues were consolidated and reported a new feature in the Flutter git repo (https://github.com/flutter/flutter/issues/116537)

    Update Flutter feature request is closed and raised new feature request in dart SDK https://github.com/dart-lang/sdk/issues/50868