universal_io 0.8.2 copy "universal_io: ^0.8.2" to clipboard
universal_io: ^0.8.2 copied to clipboard

outdated

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

Pub Package Build Status

Introduction #

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

License #

Licensed under the Apache License 2.0.

Much of the source code in this project is from Dart SDK (github.com/dart-lang/sdk/tree/master/sdk/lib/io), which was obtained under the BSD-style license of Dart SDK.

Dart SDK 2.5 breaking changes #

  • Dart SDK 2.5 (currently in the Dart SDK 'dev' channel) has various breaking changes in dart:io. Consequently this package has breaking changes too.
  • If you don't use the 2.5.0-dev-2.0 or later, see dependency constraint recommendations below.

Issues #

Similar packages #

Getting started #

1.Add a dependency #

Dart SDK 2.5.0-dev-2.0 and above #

In pubspec.yaml:

dependencies:
  universal_io: '^0.8.2'

Dart SDK 2.4.x #

In pubspec.yaml:

dependencies:
  universal_io: '>=0.7.3 <0.8.0'

2. Choose a driver (optional) #

VM/Flutter? #

  • Library "package:universal_io/io.dart" will automatically export dart:io for you.

Browser? #

  • BrowserIODriver is automatically used when you use Dart2js / devc. This is possible with "conditional imports" feature of Dart.
  • The driver implements HttpClient (with restrictions) and a few other features. If you need features such as sockets or unrestricted HTTP connections, choose one of the options below.

Chrome OS App? #

3. Use #

import 'package:universal_io/io.dart';

void main() async {
  // Use 'dart:io' HttpClient API.
  final httpClient = new HttpClient();
  final request = await httpClient.getUrl(Uri.parse("http://google.com"));
  final response = await request.close();
}

Manual #

Default driver behavior #

HTTP client #

In browser, HTTP client is implemented using dart:html HttpRequest, which uses XmlHttpRequest.

Unlike HTTP client in the standard dart:io, the browser implementation sends HTTP request only after httpRequest.close() has been called.

If a 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
URL:           http://destination.com
Origin:        http://source.com

Cross-origin request!
CORS 'credentials' mode is disabled (the browser will not send authentication like cookies).
You can enable 'credentials' mode with: request.headers.set('Authorization', "someValue")

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

HttpServer #

  • Requires sockets.

Platform #

  • In browser, variables are determined by browser APIs such as navigator.userAgent.
  • Elsewhere (e.g. Node.JS), appears like Linux environment.

Files #

  • Any attempt to use these APIs will throw UnimplementedError.

Sockets #

  • Any attempt to use these APIs will throw UnimplementedError.

Writing your own driver? #

import 'package:universal_io/io.dart';
import 'package:universal_io/driver.dart';
import 'package:universal_io/driver_base.dart';

void main() {
  exampleIODriver.enable();
}

/// Let's change 'Platform' implementation (in browser).
final exampleIODriver = IODriver(
  platformDriver: PlatformDriver(localeName:"en-US"),
).fillMissingFeaturesFrom(defaultIODriver);
212
likes
0
pub points
99%
popularity

Publisher

verified publisherdint.dev

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

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

meta, zone_local

More

Packages that depend on universal_io