跳到主要内容

avoid_web_libraries_in_flutter

不要在 Flutter Web 插件之外使用仅限 Web 的库。

描述

#

当一个非 Web 插件包中的库导入了仅限 Web 的库时,分析器会生成此诊断信息

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

示例

#

如果在非 Web 插件包中发现以下代码,分析器会生成此诊断信息,因为它导入了 dart:html

dart
import 'dart:html';

import 'package:flutter/material.dart';

class C {}

常见修复

#

如果该包不打算用作 Web 插件,则移除导入。

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

class C {}

如果该包打算用作 Web 插件,则将以下行添加到该包的 pubspec.yaml 文件中

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

有关更多信息,请参阅开发包和插件