패키지, 저장소 및 앱 카테고리 검색

GitHub 저장소dint-dev/universal_io

universal_io

크로스 플랫폼 dart:io, 브라우저 호환 HttpClient 포함.

universal_io 프로젝트 이미지
스타
101
포크
14
최근 푸시(UTC)
2025. 11. 16.
프로젝트 상태
활성

원본 README

아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.

Pub Package package publisher Github Actions CI

Overview

A cross-platform dart:io that works on all platforms, including browsers.

You can simply replace dart:io imports with package:universal_io/universal_io.dart.

Licensed under the Apache License 2.0. Some of the source code was derived from Dart SDK, which was obtained under the BSD-style license of Dart SDK. See LICENSE file for details.

APIs added on top of "dart:io"

Links

Similar packages

Getting started

1.Add dependency

In pubspec.yaml:

dependencies:
  universal_io: ^2.3.0

2.Use HTTP client

// Replace 'dart:io' imports with the following:
import 'package:universal_io/universal_io.dart';

Future<void> main() async {
  // HttpClient can now be used in browser too!
  final httpClient = newUniversalHttpClient();
  final request = await httpClient.getUrl(Uri.parse("https://dart.dev/"));
  final response = await request.close();
}

HTTP client behavior

HTTP client is implemented with XMLHttpRequest (XHR) API on browsers.

XHR causes the following differences with dart:io:

  • HTTP connection is created only after request.close() has been called.
  • Same-origin policy limitations. For making cross-origin requests, see documentation below.

Helpful error messages

When requests fail and assertions are enabled, error messages contains descriptions how to fix possible issues such as missing cross-origin headers.

The error messages look like the following:

XMLHttpRequest error.
-------------------------------------------------------------------------------
HTTP method:             PUT
HTTP URL:                http://destination.com/example
Origin:                  http://source.com
Cross-origin:            true
browserCredentialsMode:  false
browserResponseType:     arraybuffer

THE REASON FOR THE XHR ERROR IS UNKNOWN.
(For security reasons, browsers do not explain XHR errors.)

Is the server down? Did the server have an internal error?

Enabling credentials mode would enable use of some HTTP headers in both the
request and the response. For example, credentials mode is required for
sending/receiving cookies. If you think you need to enable 'credentials mode',
do the following:

    final httpClientRequest = ...;
    if (httpClientRequest is BrowserHttpClientRequest) {
      httpClientRequest.browserCredentialsMode = true;
    }

Did the server respond to a cross-origin "preflight" (OPTIONS) request?

Did the server send the following headers?
  * Access-Control-Allow-Origin: http://source.com
    * You can also use wildcard ("*").
    * Always required for cross-origin requests!
  * Access-Control-Allow-Methods: PUT
    * You can also use wildcard ("*").

Sometimes when you do cross-origin requests in browsers, you want to use CORS "credentials mode". This can be achieved with the following pattern:

import 'package:universal_io/universal_io.dart';

Future<void> main() async {
    final client = newUniversalHttpClient();
    final request = client.getUrl(Url.parse('http://example/url'));

    // Enable credentials mode
    if (request is BrowserHttpClientRequest) {
      request.browserCredentialsMode = true;
    }

    // Close request
    final response = await request.close();
    // ...
}

Streaming text responses

The underlying XMLHttpRequest (XHR) API supports response streaming only when responseType is "text". This works for responses that are UTF-8 text.

This package automatically uses responseType "text" based on value of the HTTP request header "Accept". These media types are defined BrowserHttpClient.defaultTextMimes.

If you want to disable streaming, use the following pattern:

import 'package:universal_io/universal_io.dart';

Future<void> main() async {
    final client = newUniversalHttpClient();
    if (client is BrowserHttpClient) {
      client.onBrowserHttpClientRequestClose = (request) {
        if (someCondition) {
          request.browserResponseType = 'text';
        }
      };
    }
    // ...
}
Dint GitHub avatar
GitHub 작성자

Dint ↗

Open-source Flutter/Dart packages. Administered by @gohilla. Pull are requests welcome in all projects.

언어Dart

분류

사용 중인 의존성

의존성 목록 7 개

프로젝트에 맞는 저장소를 선택하려면?

사용 목적에 따라 검색하고 개발 활동을 비교한 뒤 원본 저장소에서 요구 사항을 확인하세요.

  1. 이름, 작성자 또는 설명으로 검색하고 기술 주제로 범위를 좁힙니다.
  2. 스타, 포크, 최근 푸시를 함께 비교하세요. 스타는 관심의 지표이며 품질을 보장하지 않습니다.
  3. 사용하기 전에 GitHub에서 README, 라이선스, Issues를 확인하세요.

출처 및 관리

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗