错误:找不到 'dart:js' 导入 'dart:js';

21

我正在AppBar部分创建一个PopupMenuButton()。
这是我的文件:

import 'dart:js';
import 'package:bfdi_app/settings.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class ProfilePage extends StatefulWidget {

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'App_Name',
          ),
        actions: <Widget>[
            PopupMenuButton(
              icon: Icon(Icons.settings),
              onSelected:(value){
                if(value==0){
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context)=>SettingPage())
                  );
                }
              },
              itemBuilder: (context) => [
                    PopupMenuItem(
                      child: Text("Settings"),
                      value:0,
               ],
            ),
          ],
       ),
      }

现在我在控制台日志中面临一个错误:

Compiler message:
lib/Pages/addPost.dart:1:8: Error: Not found: 'dart:js'
import 'dart:js';
       ^
lib/Profile/profile.dart:1:8: Error: Not found: 'dart:js'
import 'dart:js';
       ^

我已经添加了依赖项dart:js,但仍然收到相同的错误。 错误: enter image description here


您能上传更多关于此错误的信息吗?您能上传整个文件吗?控制台中的错误? - no_fate
更新了错误。 - Himanshu Sharma
14个回答

16

进入您的flutter安装目录/.pub-cache/hosted/pub.dartlang.org/js-0.6.3-nullsafety.1/lib/js.dart

删除或注释掉此行:

export 'dart:js' show allowInterop, allowInteropCaptureThis;

然后再试一次


1
你真是个传奇!花了一个小时的时间清除缓存,更新包等等。 - Bram
3
这会破坏网络发布版本。 - Oliver Dixon
这不是一个强大的解决方案。缓存目录是短暂的。 - undefined

5

我刚刚使用了 "import 'package:js'",它对我有效。


5

听起来像是在尝试编译本地应用时出现了导入问题。

尝试搜索并删除不正确的 dart:js 导入 (import 'dart:js';)。

查看这个GitHub 问题 #70373


3

编辑:我已经切换到主通道,一切正常。尽管我在你的当前代码中没有看到需要使用dart.js的要求,所以你可以将其删除。


原始回答: 我相信您正在使用稳定通道,因此请尝试切换到beta通道。 打开终端并运行以下命令:

$ flutter channel beta

1
我目前在主分支中。 - Himanshu Sharma
1
我已切换至主通道,一切正常。虽然我在您当前的代码中没有看到需要使用dart.js的要求,所以您也可以将其删除。 - Ravinder Kumar
是的,我尝试将其移除,现在它可以工作了,谢谢。 - Himanshu Sharma

1

您可能无法在库支持的平台上运行该应用程序。在我的情况下,我正在Android上运行,而库仅支持Web作为输出。无需清除缓存或注释某些代码。


1

在我的情况下,只需运行flutter clean命令即可解决问题。


1
我在运行Dart GRPC项目时遇到了这个错误。 我只需运行dart fix --apply即可解决此问题。

1
引用pub.dev的内容:
  1. Depend on it
    Add this to your package's pubspec.yaml file:

    dependencies:
      js: ^0.6.2
    
  2. Install it
    You can install packages from the command line:

    with pub:

    $ pub get
    

    with flutter:

    $ flutter pub get
    

    Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

  3. Import it Now in your Dart code, you can use:

    import 'package:js/js.dart';
    

1
我不知道它是怎么出现的,但在我的一个源代码文件中,我有这个导入语句:
import 'dart:js_interop';

我已经删除了它。

另外,我不得不删除了我的Flutter项目根目录中的.dart_tool文件夹,然后重新构建了我的项目。


0

6
我添加了 import 'package:js/js.dart'; ,但出现了错误:Compiler message:/C:/Users/acer/AppData/Roaming/Pub/Cache/hosted/pub.dartlang.org/js-0.6.1+1/lib/js.dart:8:1: Error: Not found: 'dart:js'export 'dart:js' show allowInterop, allowInteropCaptureThis;^ - Himanshu Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接