Package selection guidance compiled from public package information, community data, and project usage.
universal_html provides a unified 'dart:html' API that works across all platforms, including Flutter on mobile, desktop, and server-side environments. It enables consistent HTML and XML processing regardless of the runtime, simplifying cross-platform development. This package bridges the gap between web and non-web contexts by offering a compatible interface for DOM manipulation, HTTP requests, and parsing structured content. Ideal for shared logic between web and backend services, it reduces code duplication and streamlines development workflows.
Use cases
Cross-platform web and server logic
Shared HTML/XML parsing
Server-side rendering utilities
Web component reuse in Flutter apps
Building universal Dart applications
Key features
Platform-agnostic dart:html API
Supports DOM manipulation on all platforms
Built-in XML/HTML parsing
Works seamlessly with Flutter and servers
Lightweight and minimal dependencies
Suitable for
Developers targeting multiple platforms
Backend services needing HTML processing
Flutter apps requiring web-like features
Code sharing between web and server
Projects avoiding platform-specific conditionals
Considerations
Limited browser-specific features on non-web platforms
No support for certain DOM events outside web
Performance may vary on server-side execution
Not intended for full browser emulation
Requires careful handling of async operations
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
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}');
}
}