【问题标题】:Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'URI 的目标不存在:'package:flutter_gen/gen_l10n/gallery_localizations.dart'
【发布时间】:2020-10-28 14:11:02
【问题描述】:

我现在在我的项目中使用 Flutter Gallary,这是包参考:

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

但它显示:

Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'.

我在pubspec.yaml中添加了lib:

flutter_localizations:
    sdk: flutter
intl: ^0.16.1
flutter_localized_locales: ^1.1.1

并添加l10n.yaml:

template-arb-file: intl_en.arb
output-localization-file: gallery_localizations.dart
output-class: GalleryLocalizations
preferred-supported-locales:
  - en
use-deferred-loading: false

我错过了什么吗?仍然无法正常工作,我该怎么做才能使其正常工作?这是完整的代码:

import 'package:flutter/material.dart';
import 'package:animations/animations.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';

enum BottomNavigationDemoType {
  withLabels,
  withoutLabels,
}

class BottomNavigationDemo extends StatefulWidget {
  const BottomNavigationDemo({Key key, @required this.type}) : super(key: key);

  final BottomNavigationDemoType type;

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

class _BottomNavigationDemoState extends State<BottomNavigationDemo> {
  int _currentIndex = 0;

  String _title(BuildContext context) {
    switch (widget.type) {
      case BottomNavigationDemoType.withLabels:
        return GalleryLocalizations.of(context)
            .demoBottomNavigationPersistentLabels;
      case BottomNavigationDemoType.withoutLabels:
        return GalleryLocalizations.of(context)
            .demoBottomNavigationSelectedLabel;
    }
    return '';
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;
    final textTheme = Theme.of(context).textTheme;

    var bottomNavigationBarItems = <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: const Icon(Icons.add_comment),
        label: GalleryLocalizations.of(context).bottomNavigationCommentsTab,
      ),
      BottomNavigationBarItem(
        icon: const Icon(Icons.calendar_today),
        label: GalleryLocalizations.of(context).bottomNavigationCalendarTab,
      ),
      BottomNavigationBarItem(
        icon: const Icon(Icons.account_circle),
        label: GalleryLocalizations.of(context).bottomNavigationAccountTab,
      ),
      BottomNavigationBarItem(
        icon: const Icon(Icons.alarm_on),
        label: GalleryLocalizations.of(context).bottomNavigationAlarmTab,
      ),
      BottomNavigationBarItem(
        icon: const Icon(Icons.camera_enhance),
        label: GalleryLocalizations.of(context).bottomNavigationCameraTab,
      ),
    ];

    if (widget.type == BottomNavigationDemoType.withLabels) {
      bottomNavigationBarItems = bottomNavigationBarItems.sublist(
          0, bottomNavigationBarItems.length - 2);
      _currentIndex =
          _currentIndex.clamp(0, bottomNavigationBarItems.length - 1).toInt();
    }

    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text(_title(context)),
      ),
      body: Center(
        child: PageTransitionSwitcher(
          child: _NavigationDestinationView(
            // Adding [UniqueKey] to make sure the widget rebuilds when transitioning.
            key: UniqueKey(),
            item: bottomNavigationBarItems[_currentIndex],
          ),
          transitionBuilder: (child, animation, secondaryAnimation) {
            return FadeThroughTransition(
              child: child,
              animation: animation,
              secondaryAnimation: secondaryAnimation,
            );
          },
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        showUnselectedLabels:
        widget.type == BottomNavigationDemoType.withLabels,
        items: bottomNavigationBarItems,
        currentIndex: _currentIndex,
        type: BottomNavigationBarType.fixed,
        selectedFontSize: textTheme.caption.fontSize,
        unselectedFontSize: textTheme.caption.fontSize,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        selectedItemColor: colorScheme.onPrimary,
        unselectedItemColor: colorScheme.onPrimary.withOpacity(0.38),
        backgroundColor: colorScheme.primary,
      ),
    );
  }
}

class _NavigationDestinationView extends StatelessWidget {
  _NavigationDestinationView({Key key, this.item}) : super(key: key);

  final BottomNavigationBarItem item;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        ExcludeSemantics(
          child: Center(
            child: Padding(
              padding: const EdgeInsets.all(16),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(8),
                child: Image.asset(
                  'assets/demos/bottom_navigation_background.png',
                  package: 'flutter_gallery_assets',
                ),
              ),
            ),
          ),
        ),
        Center(
          child: IconTheme(
            data: const IconThemeData(
              color: Colors.white,
              size: 80,
            ),
            child: Semantics(
              label: GalleryLocalizations.of(context)
                  .bottomNavigationContentPlaceholder(
                item.label,
              ),
              child: item.icon,
            ),
          ),
        ),
      ],
    );
  }
}

当我运行命令flutter clean &amp;&amp; flutter run 时,显示结果:

[dolphin@MiWiFi-R4CM-srv]~/AndroidStudioProjects/Cruise% flutter clean && flutter run 
Attempted to generate localizations code without having the flutter: generate flag turned on.
Check pubspec.yaml and ensure that flutter: generate: true has been added and rebuild the project. Otherwise, the localizations source code will not be
importable.
Generating synthetic localizations package has failed.

【问题讨论】:

  • flutter clean &amp;&amp; flutter run。我想它会起作用的
  • [dolphin@MiWiFi-R4CM-srv]~/AndroidStudioProjects/Cruise% flutter clean &amp;&amp; flutter run Attempted to generate localizations code without having the flutter: generate flag turned on. Check pubspec.yaml and ensure that flutter: generate: true has been added and rebuild the project. Otherwise, the localizations source code will not be importable. Generating synthetic localizations package has failed.

标签: flutter


【解决方案1】:

我关注了 Flutter 官方文档 (https://flutter.dev/docs/development/accessibility-and-localization/internationalization),但遇到了和你一样的问题。我首先尝试了“颤振升级”。问题依然存在。

之后,我尝试关闭我的IDE(Android studio)并再次打开它,问题就解决了!

【讨论】:

  • 我认为这样的问题只能在 eclipse 中找到,而不能在 intellij 中找到......但是我们开始......重新启动有帮助。
  • 对 vscode 也有帮助
  • 应该是可以接受的答案。
【解决方案2】:

除了@Sleepingisimportant 的回答,你可以重启“Dart Analysis Server”,问题就会得到解决。

此按钮位于 Android Studio 的 Dart Analysis 选项卡中,我猜这意味着它也在 Intelij 上。

【讨论】:

  • 谢谢!!该文件在那里,但 Dart 守护进程不够聪明,无法找到它。有时关闭 IDE 并重新打开工作,这似乎总是有效。
  • 这应该是公认的答案。
【解决方案3】:

我有同样的问题,我刚刚关闭并再次打开我的文件夹,我正在使用 vs code。

【讨论】:

  • 我在 Visual Studio Code 中与 import 'package:flutter_gen/gen_l10n/app_localizations.dart' 有同样的问题,关闭窗口并重新打开后,错误消失了。
  • 这实际上对我有用
  • 我关闭并打开并工作,但经过一段时间或一些更改后,错误又回来了
【解决方案4】:

尝试在~/flutter/packages/flutter 中运行flutter update-packages

flutter update-packages

或者使用flutter upgrade 命令更新 Flutter SDK:

flutter upgrade

此命令获取当前 Flutter 频道上可用的最新版本的 Flutter SDK。

更多关于如何升级 Flutter SDK 或切换 Flutter 通道的信息:https://flutter.dev/docs/development/tools/sdk/upgrading

这将解决您的import 'package:flutter_gen/gen_l10n/gallery_localizations.dart'; 问题。

【讨论】:

    【解决方案5】:

    查看 > 命令面板,然后输入 Dart: Restart Analysis Server。

    【讨论】:

      【解决方案6】:

      在我们的 pubspec.yaml 的底部,您应该将 generate 设置为 true...

      # The following section is specific to Flutter.
      flutter:
      
        # The following line ensures that the Material Icons font is
        # included with your application, so that you can use the icons in
        # the material Icons class.
        uses-material-design: true
        generate: true
      

      【讨论】:

        【解决方案7】:

        我遇到了同样的问题,我所做的是首先在终端中运行 flutter clean 命令。之后,我再次通过flutter run 命令运行了颤振。它奏效了。

        【讨论】:

          【解决方案8】:

          我通过将这两行添加到 pubspec.yaml 文件中来使导入工作:

          cupertino_icons: ^0.1.3 
          flutter_gallery: ^2.4.0+20400
          

          第一行实际上是替换原来的 Cupertino-icons 依赖,即 1.0.0 版本,而 Flutter Gallery 依赖需要一个不同的版本,即 0.1.3。

          然后使用 'flutter pub get' 更新导入

          此站点here 列出了所有可能的flutter_gallery 导入。

          【讨论】:

          • 将 intl: ^0.17.0 添加到依赖项为我解决了这个问题
          【解决方案9】:

          如果您正在使用包 flutter_gen,则需要将其从 pubscpec.yaml 中删除以解决冲突。

          【讨论】:

          • 就是这样,谢谢
          【解决方案10】:

          flutter pub get 如果还没有这样做,也可以提供帮助。这在向 pubspec.yaml 添加新内容后很重要

          【讨论】:

            【解决方案11】:

            您可能需要关闭并重新打开您的 IDE,以便它重新分析您的代码库。尤其是 VS Code 有时无法识别代码库的更改,但关闭并重新打开 IDE 会触发对代码的重新索引/分析,并且应该可以解决此幻像错误。

            【讨论】:

              【解决方案12】:

              我刚刚添加l10n.yaml后解决了,

              1. 重新启动您的项目
              2. 运行:

              颤抖干净

              颤振酒吧获取

              【讨论】:

                【解决方案13】:

                如果有人打开已添加本地化的现有项目。 这个错误总是来的。 导入“包:flutter_gen/gen_l10n/app_localizations.dart”;不存在

                所以在终端中运行这个命令: flutter pub 添加flutter_gen

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-04-03
                  • 2021-06-12
                  • 2018-12-23
                  • 2019-01-19
                  • 2021-04-21
                  • 1970-01-01
                  • 2022-11-22
                  • 2022-10-16
                  相关资源
                  最近更新 更多