避免在Flutter Web插件包之外使用仅Web库 [英] Avoid using web-only libraries outside Flutter web plugin packages

查看:600
本文介绍了避免在Flutter Web插件包之外使用仅Web库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Flutter应用,试图在网络上工作。其中一部分包含一些特定于网络的代码:

I'm building a Flutter app that I am trying to make work on the web. Part of it contains some web specific code:

import 'dart:html' as html;
import 'package:flutter/foundation.dart';

class DownloadViewModel extends ChangeNotifier {
  static const String url = 'https://example.com/api/v1/app/myapp_1.0.0.apk';
  void onAndroidDownloadPressed() {
    html.window.open(url, 'AndroidApp');
  }
}

但是 dart:html 导入会出现以下错误:

However the dart:html import gives the following error:


避免在Flutter Web插件包之外使用仅Web库

Avoid using web-only libraries outside Flutter web plugin packages

警告的较长版本如下:


避免使用网络库, dart:html dart:js dart:js_util 不是Web插件。在网络环境之外不支持这些库。依赖于它们的功能
会在Flutter移动设备上运行时失败,并且在Flutter网站上通常不鼓励使用

Avoid using web libraries, dart:html, dart:js and dart:js_util in Flutter packages that are not web plugins. These libraries are not supported outside a web context; functionality that depends on them will fail at runtime in Flutter mobile, and their use is generally discouraged in Flutter web.

Web库访问权限为允许用于:

Web library access is allowed in:

plugin packages that declare web as a supported context

否则,导入 dart:html dart:js dart:js_util

警告。这实际上阻止了构建Android或iOS应用程序(即使无法从非Web Flutter应用程序访问此方法也是如此)。

And it's not just a warning. This actually prevents building an Android or iOS app (even though this method isn't accessible from non-Web Flutter apps).

我发现的唯一解决方案是在为Android和iOS进行构建时注释掉导入,然后在为Web进行构建时取消注释。有更好的解决方案吗?

The only solution I've figured out is to comment out the import when I am building for Android and iOS and then uncomment it when I am building for the web. Is there a better solution?

推荐答案

使用 universal_html 软件包。它支持浏览器,Dart VM和Flutter,并且是 dart:html 和其他与Web相关的库的替代品。

Use the universal_html package. It supports the browser, Dart VM, and Flutter and is a stand-in replacement for dart:html and other web related libraries.

dependencies:
  universal_html: ^1.2.1

然后代替使用 import'dart:html'作为html; 可以使用以下导入:

Then instead of using import 'dart:html' as html; you can use the following import:

import 'package:universal_html/html.dart' as html;

对于那些因其他相关的网络导入问题而来到此页面的用户(例如 dart:js ),此插件还支持以下导入:

For those who came to this page for other related web import problems (like dart:js), this plugin also supports the following imports:

import 'package:universal_html/driver.dart';
import 'package:universal_html/html.dart';
import 'package:universal_html/indexed_db.dart';
import 'package:universal_html/js.dart';
import 'package:universal_html/js_util.dart';
import 'package:universal_html/prefer_sdk/html.dart';
import 'package:universal_html/prefer_sdk/indexed_db.dart';
import 'package:universal_html/prefer_sdk/js.dart';
import 'package:universal_html/prefer_sdk/js_util.dart';
import 'package:universal_html/prefer_sdk/svg.dart';
import 'package:universal_html/prefer_sdk/web_gl.dart';
import 'package:universal_html/prefer_universal/html.dart';
import 'package:universal_html/prefer_universal/indexed_db.dart';
import 'package:universal_html/prefer_universal/js.dart';
import 'package:universal_html/prefer_universal/js_util.dart';
import 'package:universal_html/prefer_universal/svg.dart';
import 'package:universal_html/prefer_universal/web_gl.dart';
import 'package:universal_html/svg.dart';
import 'package:universal_html/web_gl.dart';

这篇关于避免在Flutter Web插件包之外使用仅Web库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆