universal_html는 모바일, 데스크톱 및 서버 측 환경에서 Flutter를 포함한 모든 플랫폼에서 작동하는 통합된 'dart:html' API를 제공합니다. 실행 환경에 관계없이 일관된 HTML 및 XML 처리를 가능하게 하여 크로스플랫폼 개발을 간소화합니다. 이 패키지는 DOM 조작, HTTP 요청, 구조화된 콘텐츠 파싱을 위한 호환성 있는 인터페이스를 제공함으로써 웹과 비웹 환경 사이의 격차를 메웁니다. 웹과 백엔드 서비스 간에 공유 로직을 사용할 때 이상적이며, 코드 중복을 줄이고 개발 워크플로우를 단순화합니다.
사용 사례
다중 플랫폼 웹 및 서버 로직
공유 HTML/XML 구문 분석
서버 측 렌더링 유틸리티
Flutter 앱에서 웹 컴포넌트 재사용
일반적인 Dart 애플리케이션 빌드
주요 기능
플랫폼에 관계없는 dart:html API
모든 플랫폼에서 DOM 조작을 지원합니다
내장된 XML/HTML 파싱
Flutter 및 서버와 원활하게 작동합니다
경량이며 최소한의 종속성
적합한 대상
다중 플랫폼을 대상으로 하는 개발자
HTML 처리가 필요한 백엔드 서비스
웹 유사 기능을 필요로 하는 Flutter 앱
웹과 서버 간의 코드 공유
플랫폼별 조건을 피하는 프로젝트
고려 사항
웹이 아닌 플랫폼에서 브라우저 고유 기능이 제한됨
웹 외부의 일부 DOM 이벤트에 대한 지원 없음
서버 측 실행 시 성능이 달라질 수 있습니다
전체 브라우저 에뮬레이션을 위한 것이 아님
비동기 작업을 신중하게 처리해야 합니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Introduction
A cross-platform replacement for dart:html (or package:web):
Eases cross-platform development
You can use this package in browsers, mobile, desktop, and server-side VM, and Node.JS.
Just replace dart:html / package:web imports with package:universal_html/universal_html.dart. Normal
dart:html will continue to be used when application run in browsers.
Extensive support for processing HTML and XML documents
The project is licensed under the Apache License 2.0. Some of the source code was adopted
from the original dart:html in Dart SDK,
which is documented in the relevant files.
EventSource requests from real browsers are typically authenticated using cookies.
If you want to add cookies or customize other HTTP headers, you need to use
EventSourceOutsideBrowser:
import'package:universal_html/universal_html.dart';
import'dart:io'showCookie;
Future<void> main() async {
final eventSource = EventSource('http://example.com/events');
// The following block will NOT be executed in browsers.// Because compiler can infer instances of EventSourceOutsideBrowser are never constructed,// it will not appear in Javascript either.if (eventSource isEventSourceOutsideBrowser) {
eventSource.onHttpClientRequest = (eventSource, request) {
request.headers.set('Authorization', 'example');
request.cookies.add(Cookie('name', 'value'));
};
eventSource.onHttpClientResponse = (eventSource, request, response) {
// ...
};
}
awaitfor (var message in eventSource.onMessage) {
print('Event:');
print(' type: ${message.type}');
print(' data: ${message.data}');
}
}