# universal_io 0.3.0

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




## Installing
```
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'universal_io:0.3.0'
```
Published: 2019-05-31T13:27:13.026878Z
Expected archive SHA-256: 0b8f764b4e55e89c26c5de3665c15d7c30818ec14b6b37763cfd8ee298a054e5

Available in cache; verification: upstream_checksum
## Direct dependencies
- ip: ^0.1.1
- meta: ^1.1.7
- raw: ^0.2.0
- typed_data: ^1.1.6
- zone_local: ^0.1.0

## Package README (untrusted upstream documentation)
# Introduction
A cross-platform version of [dart:io](https://api.dartlang.org/stable/2.1.1/dart-io/dart-io-library.html).

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](LICENSE).
Much of the source code was adopted from the original 'dart:io' in [Dart SDK](https://github.com/dart-lang/sdk),
which was licensed under a BSD-style license.

## Issues
  * Found issues? Report them at the [Github issue tracker](https://github.com/gohilla/dart-universal_io/issues).
  * Have a fix? [Create a pull request](https://github.com/gohilla/dart-universal_io/pull/new/master)!

# Getting started
## 1.Add a dependency
In `pubspec.yaml`:
```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?
    * [universal_io_plugin_chrome_os](https://github.com/terrier989/dart-universal_io_plugin_chrome_os)
  * Node.JS? Google Cloud Functions?
    * [universal_io_plugin_node](https://github.com/terrier989/dart-universal_io_plugin_node)
  * A backend + GRPC messaging?
    * [universal_io_plugin_grpc](https://github.com/terrier989/dart-universal_io_plugin_grpc)

## 3. Use

```dart
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](https://developer.mozilla.org/en/docs/Web/API/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
  * Implemented using [package:ip](https://github.com/gohilla/dart-ip).

### 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?
```dart
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







 
