Rules / Correctness / avoid-web-libraries-in-flutter
avoid-web-libraries-in-flutter
Disallow importing web-only dart: libraries from Flutter code.
Flags any import of dart:html, dart:js, dart:js_util, or a
dart:js_interop* library. These libraries exist only on the web platform,
so code that depends on them fails to compile or run on Android, iOS,
desktop, and embedded targets. A Flutter package that reaches for them
silently forfeits portability across every platform Flutter otherwise
supports. Use platform-neutral APIs, or isolate web-specific code behind a
conditional import so non-web builds receive a compatible implementation.
Invalid
example.dartdart
// Bad: imports of web-only dart libraries.
import 'dart:html';
import 'dart:js';
import 'dart:js_util';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
void main() {}
Avoid using web-only libraries in Flutter code; they are not portable across platforms.
1// Bad: imports of web-only dart libraries.
2import 'dart:html';
∙
Avoid using web-only libraries in Flutter code; they are not portable across platforms.
2import 'dart:html';
3import 'dart:js';
∙
Avoid using web-only libraries in Flutter code; they are not portable across platforms.
3import 'dart:js';
4import 'dart:js_util';
∙
Avoid using web-only libraries in Flutter code; they are not portable across platforms.
4import 'dart:js_util';
5import 'dart:js_interop';
∙
Avoid using web-only libraries in Flutter code; they are not portable across platforms.
5import 'dart:js_interop';
6import 'dart:js_interop_unsafe';
∙
Valid
example.dartdart
// Good: portable imports with no web-only libraries.
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
void main() {}
How to configure
Set the severity of avoid-web-libraries-in-flutter in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"correctness": {
"avoid-web-libraries-in-flutter": "error"
}
}
}
}