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

Dol*_*hin 6 flutter

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

import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
Run Code Online (Sandbox Code Playgroud)

但它显示:

Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'.
Run Code Online (Sandbox Code Playgroud)

我在pubspec.yaml

flutter_localizations:
    sdk: flutter
intl: ^0.16.1
flutter_localized_locales: ^1.1.1
Run Code Online (Sandbox Code Playgroud)

并补充说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
Run Code Online (Sandbox Code Playgroud)

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

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,
            ),
          ),
        ),
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行命令时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.
Run Code Online (Sandbox Code Playgroud)

Sha*_*nem 71

除了@Sleepingisimportant 答案之外,您可以重新启动“Dart Analysis Server”,问题将得到解决。

重新启动 Dart 分析服务器

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

在此输入图像描述

  • 这应该是公认的答案。 (2认同)

Moh*_*eda 25

我只是在添加 l10n.yaml 后解决了这个问题,然后执行了以下操作:

  1. 重新启动你的项目
  2. 跑步:

扑干净

扑扑酒吧得到

  • 伙计,这对我有帮助,最好先这样做,然后再添加所选答案中的依赖项 (2认同)
  • 也帮助了我!不明白为什么 flutter clean 可以解决这个问题,但无论如何...... (2认同)

小智 22

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


小智 21

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


小智 19

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

  • 这也是我的问题。基本上,“intl”包在假的“flutter_gen”包下生成本地化代码,但 pub.dev 上也有一个真正的“flutter_gen”包。如果您安装后者,它将优先并隐藏生成的本地化代码,因此它看起来不存在。 (4认同)

小智 11

我通过在主文件夹中的终端上运行来解决它:

flutter gen-l10n
Run Code Online (Sandbox Code Playgroud)


Eri*_*ier 8

尝试跑flutter update-packages进去~/flutter/packages/flutter

\n
flutter update-packages\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
flutter upgrade\n
Run Code Online (Sandbox Code Playgroud)\n

此命令获取当前 Flutter 通道上可用的最新版本的 Flutter SDK\xe2\x80\x99s。

\n

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

\n

这将修复你的import \'package:flutter_gen/gen_l10n/gallery_localizations.dart\';问题。

\n


小智 8

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

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

  • 对 vscode 也有帮助 (10认同)
  • 我以为这样的问题只能在 eclipse 中找到,而不能在 intellij 中找到...但是我们开始了...重新启动有帮助。 (7认同)

Ed *_*ers 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
Run Code Online (Sandbox Code Playgroud)


小智 7

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


rai*_*ner 5

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

cupertino_icons: ^0.1.3 
flutter_gallery: ^2.4.0+20400
Run Code Online (Sandbox Code Playgroud)

第一行实际上是替换原始的 cupertino-icons 依赖项(版本 1.0.0),而 flutter gallery 依赖项需要不同的版本(0.1.3)。

然后用“flutter pub get”更新导入

网站列出了所有可能的 flutter_gallery 导入。


小智 5

对于那些从其他人那里继承该项目的人,请在任何其他试用之前运行以下命令:

flutter gen-l10n
Run Code Online (Sandbox Code Playgroud)