Skip to content

Use of http package should be moved to dio #1927

Closed

Description

@lukehutch

When I demo'd my app to the Serverpod team, numerous times queries took 1 or more seconds (the worst response time was about 8 seconds). This is for only 50 items in the database.

I have looked into this more closely, and the problem is definitely not happening on the server (total endpoint runtime is consistently low). The problem happens at this line, in serverpod_client_io.dart:

      var response = await request.close().timeout(connectionTimeout);

I have absolutely no idea why closing a request would take this long, but profiling in Dart is utterly useless and broken (I tried), and the http library is nearly impossible to properly debug because it was written back before async/await (even before null-safety and generics...).

I have had other bad experiences with http before, and I have seen numerous reports online of other people running into problems with http. The Dart team don't seem interested in rewriting it or even updating it to modern Dart.

I filed a bug about this here, but I don't anticipate seeing any action on this:

dart-lang/http#1134

The best fix for Serverpod is almost certainly to move from http to dio for sending requests to the server.

Activity

  1. lukehutch commented on Feb 17, 2024

    @lukehutch
    ContributorAuthor

    After all the work of #1928, the problem is not fixed (Dio.post shows the same erratic timing behavior, and I assume Dio is not using HttpClient at the core).

    (In spite of this, it's still probably a good idea to use dio rather than http, since the http code is a mess...)

  2. lukehutch commented on Feb 17, 2024

    @lukehutch
    ContributorAuthor

    I tried debugging this on the server too, at the point where the requests came in to HttpServer, but the erratic delay problem is not there either.

    It looks like the delay issue is in the Android emulator. If I run with the same debug build on a physical device, against the same server instance, the response times are no longer erratic.

    I'll close this, but I will leave the PR open, since the dio port is probably a worthwhile improvement.

  3. lukehutch commented on Mar 30, 2024

    @lukehutch
    ContributorAuthor

    For the record, here is the reason why HTTP requests can be very slow in Flutter:

    dart-lang/sdk#50868

  4. EchoEllet commented on May 27, 2026

    @EchoEllet
    Contributor

    Serverpod is almost certainly to move from http to dio for sending requests to the server.

    I wouldn't hardcode any implementation. Instead, it probably should be abstracted.

    Both dio and http depend on dart:io so moving to dio usually does not fix the core issue.

    Alternative: rhttp

    Using Dart's http package allows the app developers to easily swap the implementation depending on the platform, e.g., cupertino_http, cronet_http, rhttp, dio.

    https://pub.dev/packages/rhttp#compatibility-layer

    http package's API design, while it comes with fewer features, is at least slightly more polished than dio.

    Tip

    Edit: It seems this was already addressed in #5134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions