Web-only library imported from shared Flutter code
ID |
dart.portability.avoid_web_libraries_in_flutter |
Severity |
low |
Remediation Complexity |
hard |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Portability |
Language |
Dart |
Description
Reports an import or export of a library that exists only on the web platform - dart:html, dart:js, dart:js_util, dart:js_interop, dart:svg, dart:web_audio, dart:web_gl, dart:indexed_db, package:web or package:js.
Rationale
These libraries are implemented by the browser, so they are unavailable on the Dart VM and on the mobile and desktop Flutter engines. A file that imports one compiles only for web, and the failure surfaces as a build error on every other target rather than as a runtime fallback - which means a single such import in shared code pins the whole application to the web.
The following code illustrates the pattern detected by this rule:
// FLAGGED: Web-only library imported from shared Flutter code
import 'dart:html';
Remediation
Use the Flutter and platform-neutral equivalents in shared code: package:http or dart:io for requests, dart:ui and the widget layer for rendering, flutter_svg for SVG, and a plugin (shared_preferences, url_launcher, audioplayers) for the rest. When web interop is genuinely required, isolate it behind a conditional import (import 'stub.dart' if (dart.library.js_interop) 'web.dart';) or put it in the web implementation of a federated plugin, so the shared code stays portable.