代码之家  ›  专栏  ›  技术社区  ›  nick.tdr

无法为dart导入intl包

  •  12
  • nick.tdr  · 技术社区  · 8 年前

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

    My pubspec.yaml file

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

    import 'package:intl/intl.dart';
    

    我还使用颤振升级命令从终端升级了颤振。

    7 回复  |  直到 8 年前
        1
  •  32
  •   FreshD    6 年前

    以下是一些我认为与flutter中的依赖关系有关的步骤。

    1. 这件事 pubspec.yaml 就是在pub get开始工作之前,必须将其保存为CRTL/CMD+S。从IDE运行pub get不会自动保存文件。

    2. 试试跑步 flutter clean 然后跑 flutter pub get . 有时候,当你删除插件之类的依赖项时, .build 文件夹没有被正确清理,所以你必须手动清理。

    3. 您可以尝试通过运行 flutter pub cache repair

    4. 有时候重启IDE也可以解决这个问题。

        2
  •  4
  •   Ketan Ramteke    7 年前

    面临同样的问题,如果您正在导入

    import "package:intl/intl_browser.dart";
    

    然后你会得到这个错误,看起来与颤振捆绑在一起的dart SDK没有所需的依赖项。

    所以只使用

    import 'package:intl/intl.dart';
    

    它应该可以正常工作。

        3
  •  3
  •   Alessandro Ornano    7 年前

    项目清理后,可能会出现如下错误:

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

    你可以清理你的缓存,让所有你想刷新你的颤振项目:

    - 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
  •  2
  •   swift    6 年前

    在“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
  •  2
  •   Ramu Ramasani    6 年前

    它需要使用 控制键 + s 有时可能不管用。保存后需要重新启动IDE吗 pubspec.yaml 文件

        6
  •  0
  •   Dharman vijay    5 年前

    我在VisualStudio代码和最新稳定版本的Flatter上也有同样的问题。之前的答案对我的情况都没有帮助。

    跑步 pub cache repair ,这是一些答案建议的,自从它开始下载所有的颤振软件包以来,我的所有数据都用光了 不管我是否用过 没有解决任何问题。

    对我有效的解决方案是:

    而不是跑步 flutter pub get package_name 在终端中,我必须手动将包添加到“pubspec.yaml”文件中的依赖项下,如下所示:

    dependencies:
       package_name: ^1.1.0
    

    并保存它。

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

        7
  •  0
  •   Daniel Porteous    5 年前

    我结合了这个问题和 here .

    第一,进口 intl_standalone 在你的 main.dart 不 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添加到DEP中,如下所示:

    dependencies:
      flutter:
        sdk: flutter
      intl:
    

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

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

    有了这个,我可以在构建Android和iOS系统时使用它。