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

outdated

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

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 {
  // ...
}
211
likes
0
pub points
99%
popularity

Publisher

verified publisherdint.dev

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

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

ip, meta, raw, typed_data, zone_local

More

Packages that depend on universal_io