universal_io 2.0.0-nullsafety.0 copy "universal_io: ^2.0.0-nullsafety.0" to clipboard
universal_io: ^2.0.0-nullsafety.0 copied to clipboard

outdated

Cross-platform 'dart:io' that works in all platforms (browsers, Flutter, and VM).

Pub Package Github Actions CI

Overview #

A cross-platform dart:io that works in all platforms (browsers, Flutter, and VM).

The API is exactly the same API as dart:io. You can simply replace dart:io imports with package:universal_io/io.dart.

Licensed under the Apache License 2.0. Much of the source code is derived from Dart SDK, which was obtained under the BSD-style license of Dart SDK. See LICENSE file for details.

Similar packages #

Getting started #

pubspec.yaml #

dependencies:
  universal_io: ^2.0.0

main.dart #

import 'package:universal_io/io.dart';

Future<void> main() async {
  // HttpClient works
  final httpClient = HttpClient();
  final request = await httpClient.getUrl(Uri.parse("http://google.com"));
  final response = await request.close();
}

In some situations, Dart development tools (your IDE) may give warnings, but your application will compile fine. You can try to eliminate warnings by importing "package:universal_io/prefer_universal/io.dart" or "package:universal_io/prefer_sdk/io.dart"

Browser driver #

HTTP client #

HTTP client is implemented using XMLHttpRequest (XHR) (in dart:html, the class is HttpRequest).

XHR causes the following differences with dart:io:

  • HTTP connection is created only after request.close() has been called.
  • Same-origin policy limitations. For making cross-origin requests, see documentation below.

Cross-origin requests #

If any cross-origin request fails, error message contains a detailed description how to fix possible issues like missing cross-origin headers. The error messages look like:

BrowserHttpClient received an error from XMLHttpRequest (which doesn't tell
reason for the error).

HTTP method:      PUT
HTTP URL:         http://destination.com/path/example
Origin:           http://source.com
Cross-origin:     true
Credentials mode: true

Cross-origin request!
CORS 'credentials mode' is disabled (the browser will not send cookies).
You can enable 'credentials mode' with:

    if (httpRequest is BrowserHttpClientRequest) {
      httpRequest.credentialsMode = BrowserHttpClientCredentialsMode.include;
    }

Did the server send the following mandatory headers?
  * Access-Control-Allow-Origin: http://source.com
    * OR '*'
  * Access-Control-Allow-Methods: PUT

Sometimes when you do cross-origin requests in browsers, you want to use CORS "credentials mode". This can be achieved with the following pattern:

Future<void> main() async {
    final client = HttpClient();
    final request = client.getUrl(Url.parse('http://host/path'));
    if (request is BrowserHttpClientRequest) {
      request.browserCredentialsMode = true;
    }
    final response = await request.close();
    // ...
}

Streaming text responses #

The underlying XMLHttpRequest (XHR) API supports response streaming only when responseType is "text". If HTTP request header "Accept" contains only text MIMEs ("text/plain", etc.), this package uses responseType "text".

You can manually set response type:

Future<void> main() async {
    // ...

    // Change response type
    if (request is BrowserHttpClientRequest) {
      request.browserResponseType = 'text';
    }

    // Stream chunks
    final response = await request.close();
    response.listen((chunk) {
      // ...
    });
}

Platform #

The implementation supports APIs such as:

  • Platform.isWindows
  • Platform.operatingSystem
  • Platform.locale
212
likes
0
pub points
99%
popularity

Publisher

verified publisherdint.dev

Cross-platform 'dart:io' that works in all platforms (browsers, Flutter, and VM).

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

collection, crypto, meta, typed_data, zone_local

More

Packages that depend on universal_io