# universal_io 0.8.5

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




## Installing
```
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'universal_io:0.8.5'
```
Published: 2019-12-18T22:57:29.460913Z
Expected archive SHA-256: 50a660f95189e042ea5df3080302f6513a28139ba036ff92185d9705fa874883

Available in cache; verification: upstream_checksum
## Direct dependencies
- meta: ^1.1.0
- zone_local: >=0.1.1 <2.0.0

## Package README (untrusted upstream documentation)
[![Pub Package](https://img.shields.io/pub/v/universal_io.svg)](https://pub.dartlang.org/packages/universal_io)
[![Github Actions CI](https://github.com/dart-io-packages/universal_io/workflows/Dart%20CI/badge.svg)](https://github.com/dart-io-packages/universal_io/actions?query=workflow%3A%22Dart+CI%22)

# Introduction
A cross-platform _dart:io_ that works in browsers, Flutter, and VM.

## License
Licensed under the [Apache License 2.0](LICENSE).

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

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

## Similar packages
  * [universal_html](https://pub.dev/packages/universal_html) (cross-platform _dart:html_)

# Getting started
## 1.Add a dependency
In `pubspec.yaml`:
```yaml
dependencies:
  universal_io: ^0.8.5
```

## 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?
  * [universal_io_driver_chrome_os](https://github.com/terrier989/dart-universal_io_driver_chrome_os)

## 3. Use

```dart
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](https://developer.mozilla.org/en/docs/Web/API/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 cookies).
    You can enable 'credentials mode' with:

        if (httpRequest is BrowserLikeHttpRequest) {
          httpRequest.useCorsCredentials = true;
        }
    
    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?
```dart
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);
```
## Changelog
# 0.8.5
  * Raised minimum SDK to 2.6 and upgraded dependencies.
  * Changed how CORS credentials mode is enabled. It was previously enabled with a header, but now
    we introduced subclasses for HttpClient and HttpClientRequest. This is a breaking change, but we
    decided not to bump the major version number.
  * Improved analysis and test settings.

# 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](https://github.com/dart-lang/sdk/blob/master/CHANGELOG.md).
    * 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.






 
