Viewing version 2.0.0-nullsafety.2. Open the latest package page.

universal_io

2.0.0-nullsafety.2

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

Download archive

Readme

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.

Links

Similar packages

Getting started

pubspec.yaml

dependencies:
  universal_io: ^2.0.0-nullsafety.1

main.dart

import 'package:universal_io/io.dart';

Future<void> main() async {
  // HttpClient can be used in browser too!
  final httpClient = HttpClient();
  final request = await httpClient.getUrl(Uri.parse("http://example/url"));
  final response = await request.close();
}

Browser driver

HTTP client

HTTP client is implemented with 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.

Helpful error messages

When requests fail and assertions are enabled, error messages contains descriptions how to fix possible issues such as missing cross-origin headers.

The error messages look like the following:

XMLHttpRequest error.
-------------------------------------------------------------------------------
HTTP method:             PUT
HTTP URL:                http://destination.com/example
Origin:                  http://source.com
Cross-origin:            true
browserCredentialsMode:  false
browserResponseType:     arraybuffer

THE REASON FOR THE XHR ERROR IS UNKNOWN.
(For security reasons, browsers do not explain XHR errors.)

Is the server down? Did the server have an internal error?

Enabling credentials mode would enable use of some HTTP headers in both the
request and the response. For example, credentials mode is required for
sending/receiving cookies. If you think you need to enable 'credentials mode',
do the following:

    final httpClientRequest = ...;
    if (httpClientRequest is BrowserHttpClientRequest) {
      httpClientRequest.browserCredentialsMode = true;
    }

Did the server respond to a cross-origin "preflight" (OPTIONS) request?

Did the server send the following headers?
  * Access-Control-Allow-Origin: http://source.com
    * You can also use wildcard ("*").
    * Always required for cross-origin requests!
  * Access-Control-Allow-Methods: PUT
    * You can also use wildcard ("*").

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://example/url'));

    // Enable credentials mode
    if (request is BrowserHttpClientRequest) {
      request.browserCredentialsMode = true;
    }

    // Close request
    final response = await request.close();
    // ...
}

See source code.

Streaming text responses

The underlying XMLHttpRequest (XHR) API supports response streaming only when responseType is "text". This package automatically uses responseType "text" in some cases based on value of the HTTP request header "Accept".

If you want to always have arraybuffer / text responses, use:

Future<void> main() async {
    final client = HttpClient();
    final request = client.getUrl(Url.parse('http://example/url'));

    // The following causes XHR responseType 'text' to be used when request is closed.
    request.headers.set('Accept', 'text/plain');

    // Use XHR responseType 'arraybuffer'.
    if (request is BrowserHttpClientRequest) {
      request.browserResponseType = 'arrayBuffer';
    }

    // Close request
    final response = await request.close();
    // ...
}

See source code.

Platform

Supported Platform APIs include:

  • Platform.isWindows
  • Platform.operatingSystem
  • Platform.locale

See source code.

Changelog

2.0.0-nullsafety.2

  • Eliminated unnecessary dependencies.

2.0.0-nullsafety.1

  • Improves documentation.
  • Improves BrowserHttpClientException messages.
  • Deprecates libraries prefer_sdk/io.dart and prefer_universal/io.dart. Developers should import just io.dart.

2.0.0-nullsafety.0

  • The first null-safe version.
  • Makes changes in BrowserHttpClient / BrowserHttpClientRequest API:
    • The property for enabling credentials mode is now browserCredentialsMode. The default is false.
    • The property for setting response type is now browserResponseType ("arraybuffer", "text", etc.). By default, if HTTP request header "Accept" contains only text MIMEs ("text/plain", etc.), this package uses responseType "text".
    • HTTP client now has onBrowserHttpClientRequestClose for using your own logic for setting browserResponseType.
  • Removes IO adapter API.

1.0.2

  • Fixes issue #11 (InternetAddress parameter).
  • Fixes issue #12 (CORS credentials mode). Eliminates legacy, complicated behavior. Developers should choose either omit or include. Improves error messages and documentation related to it.
  • Replaces MD5/SHA1 implementations used by some of the source code copied from dart:io. It now uses package:crypto instead of implementations copied from dart:io.

1.0.0

  • Implements recent changes in 'dart:io' (Dart SDK 2.8).
  • HttpDriver is replaced by 'dart:io' HttpOverrides.
  • FileSystemDriver is replaced by 'dart:io' IOOverrides.
  • Various other driver APIs are renamed or removed.
  • BrowserLikeHttpClientRequest is now BrowserHttpClientRequest.
  • BrowserHttpClientRequest implementation is improved.

0.8.6

  • Fixed documentation and small fixes related to nodejs_io.

0.8.5

  • Raised minimum SDK to 2.6 and upgraded dependencies.
  • Changed how CORS credentials mode is enabled. It was previously enabled with a header, but now we introduced subclasses for HttpClient and HttpClientRequest. This is a breaking change, but we decided not to bump the major version number.
  • Improved analysis and test settings.

0.8.4

  • Added 'prefer_sdk/io.dart' and 'prefer_universal/io.dart' libraries for dealing with conditional export issues.
  • Library 'package:universal_io/io.dart' now exports SDK version by default.

0.8.3

  • Replaced IP address parsing with the new Uri.parseIPv4Address / Uri.parseIPv6Address.
  • Fixed missing HTTP status codes.

0.8.2

  • Fixed problems introduced by Dart SDK 2.5.0-dev-2.0.

0.8.1

  • Fixed pubspec.yaml and documented Dart SDK 2.5 breaking changes.

0.8.0

  • Updated classes to Dart 2.5. See Dart SDK documentation about the changes.
    • Various APIs now return Uint8List instead of List<int>. Examples: File, Socket, HttpClientResponse.
    • Various other breaking changes such as Cookie constructor.

0.7.3

  • Fixed the following error thrown by the Dart build system in some cases: "Unsupported conditional import of dart:io found in universal_io|lib/io.dart".

0.7.2

  • Small fixes.

0.7.1

  • Fixed various bugs.
  • Improved the test suite.

0.7.0

  • Improved driver base classes and the test suite.

0.6.0

  • Major refactoring of IODriver API.

0.5.1

  • Fixed small bugs.

0.5.0

  • Fixed various bugs.
  • Re-organized source code.
  • Eliminated dependencies by doing IP parsing in this package.
  • Improved the test suite for drivers.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'universal_io:2.0.0-nullsafety.2'
pubspec.yaml
name: universal_io
version: 2.0.0-nullsafety.2
description: Cross-platform 'dart:io' that works in all platforms (browsers, Flutter, and VM).
homepage: https://github.com/dart-io-packages/universal_io

environment:
  sdk: '>=2.12.0-133.2 <3.0.0'

dependencies:
  collection: ^1.15.0-nullsafety.4
  crypto: ^3.0.0-nullsafety.0
  meta: ^1.3.0-nullsafety.6
  typed_data: ^1.3.0-nullsafety.5

dev_dependencies:
  pedantic: ^1.10.0-nullsafety.3
  test: ^1.16.0-nullsafety.13

#dependency_overrides:
#  crypto: ^3.0.0-nullsafety.0

Version history

VersionPublishedArchiveSHA-256
0.0.1Cached · Upstream SHA-256 matchedDownloada8aa4b4f…6f259b
0.1.0Cached · Upstream SHA-256 matchedDownloadadeca974…128b76
0.1.1Cached · Upstream SHA-256 matchedDownloadb4d0cce4…efe2b0
0.2.0Cached · Upstream SHA-256 matchedDownloaddebb79ed…36d4ee
0.3.0Cached · Upstream SHA-256 matchedDownload0b8f764b…a054e5
0.4.0Cached · Upstream SHA-256 matchedDownload7f879f79…0ee63f
0.5.0Cached · Upstream SHA-256 matchedDownloada185c8ca…f5f7c2
0.5.1Cached · Upstream SHA-256 matchedDownload47881238…a13ab3
0.6.0Cached · Upstream SHA-256 matchedDownload0465c285…30fb21
0.7.0Cached · Upstream SHA-256 matchedDownload0b6ff36a…c988ef
0.7.1Cached · Upstream SHA-256 matchedDownload3fd33123…e947d1
0.7.2Cached · Upstream SHA-256 matchedDownloadab916807…e30f36
0.7.3Cached · Upstream SHA-256 matchedDownload9918c2ae…d6e492
0.8.0Cached · Upstream SHA-256 matchedDownload9e971384…0ffa98
0.8.1Cached · Upstream SHA-256 matchedDownload39dd369c…e29abc
0.8.2Cached · Upstream SHA-256 matchedDownload873f9c3e…b5629f
0.8.3Cached · Upstream SHA-256 matchedDownload0da20598…979fb2
0.8.4Cached · Upstream SHA-256 matchedDownloadb1c84301…3b7ec0
0.8.5Cached · Upstream SHA-256 matchedDownload50a660f9…874883
0.8.6Cached · Upstream SHA-256 matchedDownloade700bb7c…986502
1.0.0Cached · Upstream SHA-256 matchedDownload5da439c3…bddf21
1.0.1Cached · Upstream SHA-256 matchedDownload2c0fa418…f6cb08
1.0.2Cached · Upstream SHA-256 matchedDownload22c5875f…f9e8db
2.0.0-nullsafety.0Cached · Upstream SHA-256 matchedDownload2fe15fca…c8acd3
2.0.0-nullsafety.1Cached · Upstream SHA-256 matchedDownload2c94fd97…a42cfa
2.0.0-nullsafety.2Cached · Upstream SHA-256 matchedDownloadad7e7015…8099d1
2.0.0Cached · Upstream SHA-256 matchedDownload6ba43dc8…75397e
2.0.1Cached · Upstream SHA-256 matchedDownloadffb7b33e…21e456
2.0.2Cached · Upstream SHA-256 matchedDownload02437682…2d832b
2.0.3Cached · Upstream SHA-256 matchedDownloadee2ecc7c…bc6f23
2.0.4Cached · Upstream SHA-256 matchedDownload79f78dda…f03cef
2.0.5Cached · Upstream SHA-256 matchedDownload2730b3fc…680b85
2.1.0Cached · Upstream SHA-256 matchedDownload070d8fcc…745007
2.2.0Cached · Upstream SHA-256 matchedDownload06866290…48005d
2.2.1Cached · Upstream SHA-256 matchedDownload302df7ff…d51401
2.2.2Cached · Upstream SHA-256 matchedDownload1722b2dc…ba9aad
2.2.3Cached · Upstream SHA-256 matchedDownloadba9dde5f…2edf04
2.3.0Cached · Upstream SHA-256 matchedDownload6780711d…79530a
2.3.1Cached · Upstream SHA-256 matchedDownloadf63cbc48…9301e2

Dependencies

collection "^1.15.0-nullsafety.4"

crypto "^3.0.0-nullsafety.0"

meta "^1.3.0-nullsafety.6"

typed_data "^1.3.0-nullsafety.5"