【问题标题】:Not able to import the intl package for dart无法导入 dart 的 intl 包
【发布时间】:2018-10-29 04:39:06
【问题描述】:

您好,我正在尝试在我的颤振项目中使用 intl 包。 packages get 命令运行成功,但是当我导入包时它显示错误。

对于我的 dart 文件中的导入,我使用以下导入

import 'package:intl/intl.dart';

我还使用flutter upgrade命令从终端升级了flutter。

【问题讨论】:

  • 显示什么错误?
  • 不,它不会打印任何错误。我得到以下信息: C:\Users\personal\flutter\bin\flutter.bat --no-color packages get Running "flutter packages get" in quake_report... Process finished with exit code 0
  • 导入时说目标 uri 不存在。
  • 你试过重启你的IDE吗?
  • 谢谢伙计。重新启动 IDE 对我有用。

标签: dart flutter


【解决方案1】:

以下是一些我经常认为是 Flutter 依赖项问题的步骤。

  1. pubspec.yaml 的问题是您必须在 pub get 工作之前将其保存为 CRTL/CMD + S。从 IDE 运行 pub get 不会自动保存文件。

  2. 尝试运行flutter clean,然后运行flutter pub get。 有时,当您删除插件等依赖项时,.build 文件夹无法正确清理,因此您必须手动进行。

  3. 你可以尝试通过运行flutter pub cache repair来修复pub缓存

  4. 有时只需重新启动 IDE 也可以解决问题。

【讨论】:

  • 您是否尝试过运行 flutter clean,而不是 pub get?
  • 那么,如果这不起作用,您可以尝试 pub cache repair
  • 我尝试了以上所有方法,但对我没有用。但是感谢您编写该解决方案。如果我被卡住了,下次我可以尝试这些步骤。正如@Gunter 所说,重新启动 IDE 对我有用
  • 没问题,我把它们写下来了,因为我经常看到这个弹出窗口,通常这个步骤可以解决问题
  • 重启 IDE 比你想象的要强大得多。
【解决方案2】:

如果您要导入,也会遇到同样的问题

import "package:intl/intl_browser.dart";

然后你会得到那个错误,似乎与flutter捆绑的dart SDK没有所需的依赖项。

所以只能使用

import 'package:intl/intl.dart';

它应该可以正常工作。

【讨论】:

    【解决方案3】:

    项目清理后,您可能会遇到如下错误:

    intl_browser.dart:13:8: Error: Not found: 'dart:html'
    

    你可以清理你的缓存,让你想刷新你的flutter项目:

    - flutter clean 
    - rm -rf pubspec.lock .packages .flutter-plugins 
    - flutter pub pub cache repair
    - flutter packages get 
    

    但是,除非您在代码中注释此行,否则您无法解决:

    findSystemLocale()
    

    此行与dart:html 冲突,您可以在下面根据当前intl 来源看到:

    library intl_browser;
    
    import "dart:async";
    import "dart:html";
    import "intl.dart";
    
    // TODO(alanknight): The need to do this by forcing the user to specially
    // import a particular library is a horrible hack, only done because there
    // seems to be no graceful way to do this at all. Either mirror access on
    // dart2js or the ability to do spawnUri in the browser would be promising
    // as ways to get rid of this requirement.
    /// Find the system locale, accessed as window.navigator.language, and
    /// set it as the default for internationalization operations in the
    /// [Intl.systemLocale] variable.
    Future<String> findSystemLocale() {
      Intl.systemLocale = Intl.canonicalizedLocale(window.navigator.language);
      return new Future.value(Intl.systemLocale);
    }
    

    【讨论】:

      【解决方案4】:

      在我在“intl”之前插入一个标签后,它开始为我工作。最后一组代码看这里...

      dependencies:
        flutter:
          sdk: flutter
      
      
        # The following adds the Cupertino Icons font to your application.
        # Use with the CupertinoIcons class for iOS style icons.
        cupertino_icons: ^0.1.3
        intl: ^0.16.1
      

      【讨论】:

        【解决方案5】:

        它需要重新加载流包使用 CTRL+S 有时可能不起作用。保存pubspec.yaml文件后需要重启IDE。

        【讨论】:

        • 试过所有终端命令都不起作用。 Ctrl + S 工作。谢谢
        【解决方案6】:

        我在使用最新稳定版 Flutter 的 Visual Studio Code 上遇到了同样的问题。以前的答案对我来说都没有帮助。

        运行pub cache repair,这是一些答案所建议的,因为它开始下载所有颤振包无论我是否使用它们,都用完了我的所有数据并且没有解决任何问题.

        对我有用的解决方案:

        我不得不手动在“pubspec.yaml”文件中的依赖项下添加包,而不是在终端中运行flutter pub get package_name

        dependencies:
           package_name: ^1.1.0
        

        并保存。

        保存后,IDE 将为我获取软件包,一切正常。

        【讨论】:

          【解决方案7】:

          我结合了这个问题和here 的内容来修复它。

          首先,在您的main.dart 中导入intl_standalone,而不是intl_browser

          import 'package:intl/date_symbol_data_local.dart';
          import 'package:intl/intl.dart';
          import 'package:intl/intl_standalone.dart';
          

          然后调用必要的设置函数,像这样:

          void main() async {
            Intl.defaultLocale = await findSystemLocale();
            await initializeDateFormatting();
            runApp(MyApp());
          }
          

          确保将 intl 添加到您的部门,如下所示:

          dependencies:
            flutter:
              sdk: flutter
            intl:
          

          然后从你的项目目录中依次运行以下命令:

          flutter clean 
          rm -rf pubspec.lock .packages .flutter-plugins 
          flutter pub cache repair
          flutter pub get
          

          有了这个,我可以在构建到 Android 和 iOS 时让它工作。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-30
            • 1970-01-01
            • 2019-04-12
            • 2020-11-20
            • 2022-10-23
            • 2013-10-31
            相关资源
            最近更新 更多