简体   繁体   English

运行测试时出现“未找到:'dart:js' export 'dart:js' show allowInterop, allowInteropCaptureThis”错误

[英]“Not found: 'dart:js' export 'dart:js' show allowInterop, allowInteropCaptureThis” error when running tests

In my Flutter app, I have a file called web.dart and inside it I have webSaveAs function which saves a file to my local machine in web. In my Flutter app, I have a file called web.dart and inside it I have webSaveAs function which saves a file to my local machine in web.

@JS()
library main;

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

/// Annotate `webSaveAs` to invoke JavaScript `window.webSaveAs`
@JS('webSaveAs')
external void webSaveAs(Blob blob, String fileName);

However, when I run any test (using flutter test command) which imports widgets where I have used webSaveAs function, I get the following error:但是,当我运行任何导入我使用webSaveAs function 的小部件的测试(使用flutter test命令)时,我收到以下错误:

./../../../development/flutter/.pub-cache/hosted/pub.dartlang.org/js-0.6.3-nullsafety.3/lib/js.dart:8:1: Error: Not found: 'dart:js'
export 'dart:js' show allowInterop, allowInteropCaptureThis;
^

I am using js: ^0.6.2 from https://pub.dev/packages/js and here is the result from flutter doctor command.我正在使用js: ^0.6.2 from https://pub.dev/packages/js ,这是flutter doctor命令的结果。

╰>>> flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 1.25.0-8.3.pre, on macOS 11.2.1 20D74 darwin-x64, locale en-SG)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.5)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.3.1)
[✓] VS Code (version 1.53.2)
[✓] Connected device (1 available)

• No issues found!

Could someone help me figure this out?有人可以帮我解决这个问题吗? Thanks in advance!提前致谢!

Issue:问题:

When you execute flutter test command, Flutter will compile the code in test files to native Dart and run inside a Dart VM where there is no Javascript support. When you execute flutter test command, Flutter will compile the code in test files to native Dart and run inside a Dart VM where there is no Javascript support. Hence, it will not contain dart:js .因此,它不会包含dart:js


Solution:解决方案:

You can create a dummy file (let's say web.test.dart ) like below to mock the functions that use package:js or dart:js package methods. You can create a dummy file (let's say web.test.dart ) like below to mock the functions that use package:js or dart:js package methods.

web.test.dart

import 'package:universal_html/html.dart';

void webSaveAs(Blob blob, String fileName) {
  // You can remove the below line below and include your own implementation if you need
  throw UnimplementedError();
}

And then create a new file (let's say shared.dart ) like below.然后创建一个新文件(比如说shared.dart ),如下所示。

shared.dart

export 'web.dart' if (dart.library.io) 'web.test.dart';

When you need to use webSaveAs function, instead of importing web.dart file, you should import shared.dart . When you need to use webSaveAs function, instead of importing web.dart file, you should import shared.dart .


Explanation:解释:

dart.library.io is available in Dart VM and dart.library.js is available in web. dart.library.io is available in Dart VM and dart.library.js is available in web.

In web, if (dart.library.io) is false .在 web 中, if (dart.library.io)false Therefore, it will import web.dart which is what we want.因此,它将导入我们想要的web.dart

When running tests, if (dart.library.io) is true since the tests run in Dart VM.运行测试时, if (dart.library.io)true ,因为测试在 Dart VM 中运行。 Hence, it will import web.test.dart which won't throw any errors since it does not import package:js or dart:js . Hence, it will import web.test.dart which won't throw any errors since it does not import package:js or dart:js .


References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM