URI的目标不存在:'package:flutter_gen/gen_l10n/gallery_localizations.dart'。

81

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

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中添加了一个库:

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 && 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.

10
执行 flutter clean && flutter run。我认为这样就可以了。 - Alok
[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. - Dolphin
30个回答

97

7
我以为这样的问题只会出现在Eclipse而不是Intellij中...但是事实证明并非如此...重启帮助解决了问题。 - Stuck
9
帮助对于 VS Code 也很有用。 - Ataberk
1
这应该是被接受的答案。 - Christopher Nolan
很遗憾,这确实是最好的答案。在我看来,Dart Lint 的开发人员本可以做得更好。 - Hacker
也适用于 IntelliJ Idea Ultimate Edition(带有 Flutter 插件)。 - Georg Muehlenberg
显示剩余4条评论

70

除了@Sleepingisimportant的回答外,您可以重新启动“Dart分析服务器”,问题将得到解决。

重新启动Dart分析服务器

这个按钮在Android Studio的Dart Analysis选项卡中,我猜这也适用于Intelij。

输入图像描述


1
谢谢!文件在那里,但是Dart守护程序不够聪明,找不到它。关闭IDE并重新打开有时可以解决问题,这似乎总是有效的。 - Bugzilla
2
这应该是被接受的答案。 - Dan1ell

23
我在添加了 l10n.yaml 后解决了这个问题,然后按照以下步骤操作:
  1. 重新启动您的项目
  2. 运行:

flutter clean

flutter pub get


2
哇,这真的帮了我很多。最好先这样做,再像所选答案中那样添加依赖项。 - Nikita Shadkov
2
帮了我很多!不太明白为什么Flutter clean会解决这个问题,但无论如何... - Logemann
1
@Logemann 因为这是缓存内存的问题,只需要刷新一下就可以了,这是因为即使是来自谷歌的软件也不是完美的。 - Mohamed Reda

22

我也遇到同样的问题,我只是关闭并重新打开了文件夹,我正在使用VS Code。


我在使用 Visual Studio Code 时遇到了与 import 'package:flutter_gen/gen_l10n/app_localizations.dart' 相关的问题,但是在关闭窗口并重新打开后,错误消失了。 - Fritz Lim
1
这对我实际有效。 - Trần Minh Quang
我关闭并重新打开它,它可以工作,但是过一段时间或进行一些更改后,错误又会出现。 - Guilherme
如果项目文件夹内没有l10n.yaml文件且pubspec.yaml中generate:true,则无法正常工作。这只会浪费时间,所有错误都会在编辑器完成索引项目文件后再次显示出来。 - Elmar
喂!与重新启动Android Studio相比,这是一种更快的方法。非常感谢... :) - Jagadish Nallappa

21

打开 View > Command Palette,然后输入 Dart: Restart Analysis Server 即可重启分析服务器。


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

that's it Thanks - Adel Ben Hamadi
天哪,这就是它…… - giorgio79
3
这也是我的问题。基本上,intl包会在一个虚假的flutter_gen包下生成本地化代码,但是在pub.dev上也有一个真正的flutter_gen包。如果你安装了后者,它将优先使用并隐藏生成的本地化代码,因此它看起来好像不存在。 - rjh
哦,我的朋友,非常感谢你,你是救命恩人。非常感谢。 - Divit Vaghani
我遇到了同样的问题:尝试在你的pubspec.yaml文件中删除'generate: true',并在l10n.yaml文件中禁用'synthetic-package: false'。 - Suat Özkaya

9

我通过在主文件夹中的终端运行以下命令来解决了这个问题:

flutter gen-l10n


8

尝试在 ~/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';问题。


7

我遇到了相同的问题,我的解决办法是首先在终端中运行 flutter clean 命令。之后再次通过 flutter run 命令运行 flutter。这样做有效。


7
在我们的 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

这对我来说是个解决方案 :) 谢谢。 - bqubique

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