Skip to main content

avoid_web_libraries_in_flutter

Don't use web-only libraries outside Flutter web plugins.

Description

#

The analyzer produces this diagnostic when a library in a package that isn't a web plugin contains an import of a web-only library:

  • dart:html
  • dart:js
  • dart:js_util
  • dart:js_interop
  • dart:js_interop_unsafe
  • package:js
  • package:web

Example

#

When found in a package that isn't a web plugin, the following code produces this diagnostic because it imports dart:html:

dart
import 'dart:html';

import 'package:flutter/material.dart';

class C {}

Common fixes

#

If the package isn't intended to be a web plugin, then remove the import:

dart
import 'package:flutter/material.dart';

class C {}

If the package is intended to be a web plugin, then add the following lines to the pubspec.yaml file of the package:

yaml
flutter:
  plugin:
    platforms:
      web:
        pluginClass: HelloPlugin
        fileName: hello_web.dart

See Developing packages & plugins for more information.