universal_io 0.1.1 universal_io: ^0.1.1 copied to clipboard
Supports a subset of 'dart:io' in all platforms, including the browser.
Introduction #
A Dart package that supports a subset of dart:io in all platforms, including the browser (where it normally doesn't work).
Typically you just import "package:universal_io/io.dart" (instead of "dart:io"):
- In the Dart VM and Flutter, the library exports dart:io.
- In browser: the library exports its own, compatible classes.
Licensed under the MIT License. Much of the source code was adopted from the original 'dart:io' in Dart SDK, which was licensed under a BSD-style license. See LICENSE for all licenses.
Development #
- Star the project at: github.com/terrier989/dart-universal_io
- Report issues at the Github issue tracker.
- Create pull requests if you contribute source code.
Features #
- Platform information
- Information such as operating system, locale, and stdin/stdout. You can override the behavior.
- HTTP client
- In browser, the connection to the server is established only after you have committed request
body by calling
HttpClientRequest
methodclose()
.
- In browser, the connection to the server is established only after you have committed request
body by calling
- File access
- In browsers, access to all directories and files is denied. You can override the behavior.
- Sockets
- In most browsers, an attempt to create sockets throws UnsupportedError. You can override the behavior.
- In Chrome OS apps, we use chrome.sockets.tcp and chrome.sockets.udp APIs.
Getting started #
Add dependency #
In pubspec.yaml
:
dependencies:
universal_io: ^0.1.0
Override behavior #
import 'dart:async';
import 'package:universal_io/io.dart';
import 'package:universal_io/io_driver.dart';
void main() async {
// Set IO driver
IODriver.zoneLocal.defaultValue = new MyDriver();
// Do something
final socket = await Socket.connect("google.com", 80);
socket.close();
}
class MyDriver extends IODriver {
@override
Future<Socket> connectSocket(host, int port,
{sourceAddress, Duration timeout}) {
print("Attempting to connect to '$host:$port'");
return super.connectSocket(host, port);
}
}
Use Chrome OS driver #
import 'package:universal_io/io.dart';
import 'package:universal_io/io_driver.dart';
import 'package:universal_io_for_chrome_os/io_driver_chrome_os.dart';
void main() async {
// Use Chrome OS driver for IO
IODriver.zoneLocal.defaultValue = new ChromeIODriver();
// ...
}