Viewing version 0.3.0. Open the latest package page.

universal_io

0.3.0

Cross-platform version of 'dart:io'. Supports browser, Node.JS, and other targets.

Download archive

Readme

Introduction

A cross-platform version of dart:io.

You can just replace usages of "dart:io" with "package:universal_io/io.dart". This is what happens:

  • In browser (and other Javascript targets):
    • Exports our (only slightly modified) copy "dart:io" APIs.
    • Some features work by default in browsers. Others features need a plugin (see below).
  • In Flutter and Dart VM:
    • Exports the standard "dart:io".
    • This is accomplished with conditional imports, which is an undocumented feature of Dart.

License

Licensed under the Apache License 2.0. Much of the source code was adopted from the original 'dart:io' in Dart SDK, which was licensed under a BSD-style license.

Issues

Getting started

1.Add a dependency

In pubspec.yaml:

dependencies:
  universal_io: ^0.3.0

2. Choose a plugin (optional)

  • VM/Flutter?
    • Library "package:universal_io/io.dart" will automatically export dart:io for you.
  • Browser?
    • BrowserIODriver is automatically used when compiling with Dart2js / devc. Most importantly, it implements HttpClient (with restrictions imposed by browsers).
    • If you need things like sockets or unrestricted HttpClient, choose one of the options below.
  • Chrome OS App?
  • Node.JS? Google Cloud Functions?
  • A backend + GRPC messaging?

3. Use

import 'package:universal_io/io.dart';

void main() async {
  // Use 'dart:io' HttpClient API
  //
  // This works automatically in:
  //   * Browser (where using standard 'dart:io' would not even compile)
  //   * Flutter and VM
  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.

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

For example:

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 (cookies will NOT be supported).

If the URL is correct and the server actually responded, did the response
include the following required CORS headers?
  * Access-Control-Allow-Origin: http://source.com
    * Wildcard '*' is also acceptable.
  * Access-Control-Allow-Methods: PUT

InternetAddress

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.

Socket classes and HttpServer

  • Any attempt to use these APIs will throw UnimplementedError.

Writing your own plugin?

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

void main() {
  IODriver.zoneLocal.freezeDefault(const ExampleDriver());
  
  // Now the APIs will use your driver.
  final file = new File();
  print(file is ExampleFile);
}

class ExampleDriver extends BaseDriver {
  const ExampleDriver() : super(fileSystemDriver:const MyFileSystemDriver());
}

class ExampleFileSystemDriver extends BaseFileSystemPlugin {
  const ExampleFileSystemDriver();

  @override
  File newFile(String path) {
    return new ExampleFile();
  }
}

class ExampleFile extends BaseFile {
  // ...
}
Changelog

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'universal_io:0.3.0'
pubspec.yaml
name: universal_io
version: 0.3.0
description:
  Cross-platform version of 'dart:io'. Supports browser, Node.JS, and other targets.
homepage: https://github.com/gohilla/dart-universal_io
author: terrier989 <terrier989@gmail.com>

environment:
  sdk: '>=2.0.0 <3.0.0'

dependencies:
  ip: ^0.1.1
  meta: ^1.1.7
  raw: ^0.2.0
  typed_data: ^1.1.6
  zone_local: ^0.1.0

dev_dependencies:
  pedantic: ^1.7.0
  stream_channel: any
  test: ^1.6.3

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

ip "^0.1.1"

meta "^1.1.7"

raw "^0.2.0"

typed_data "^1.1.6"

zone_local "^0.1.0"