universal_io 0.8.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 88

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.4

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/prefer_universal/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();
}

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' instead of the library above.

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/prefer_universal/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);

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.

example/example.dart

import 'package:universal_io/prefer_universal/io.dart';

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

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  universal_io: ^0.8.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:universal_io/driver.dart';
import 'package:universal_io/driver_base.dart';
import 'package:universal_io/io.dart';
import 'package:universal_io/prefer_sdk/io.dart';
import 'package:universal_io/prefer_universal/io.dart';
import 'package:universal_io/utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

We analyzed this package on Dec 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.13.1+4

Health suggestions

Fix lib/src/io/io/socket.dart. (-0.50 points)

Analysis of lib/src/io/io/socket.dart reported 1 hint:

line 741 col 21: This function has a return type of 'void Function()', but doesn't end with a return statement.

Fix lib/src/io/io/socket_impl.dart. (-0.50 points)

Analysis of lib/src/io/io/socket_impl.dart reported 1 hint:

line 167 col 19: This function has a return type of 'void Function()', but doesn't end with a return statement.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
meta ^1.1.0 1.1.8
zone_local >=0.1.1 <2.0.0 0.1.1
Dev dependencies
async ^2.3.0
pedantic ^1.8.0
stream_channel ^2.0.0
test ^1.6.0