提问人:Dolphin 提问时间:10/28/2020 最后编辑:Dolphin 更新时间:11/7/2023 访问量:49455
URI 的目标不存在:“package:flutter_gen/gen_l10n/gallery_localizations.dart”
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/gallery_localizations.dart'
问:
我现在在我的项目中使用 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'.
我在以下位置添加了lib: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.
答:
在 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
评论
尝试在 .flutter update-packages
~/flutter/packages/flutter
flutter update-packages
或者使用以下命令更新 Flutter SDK:flutter upgrade
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';
我通过将这两行添加到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' 更新导入
此处列出了所有可能的flutter_gallery导入。
评论
我关注了 Flutter 官方文档(https://flutter.dev/docs/development/accessibility-and-localization/internationalization),但遇到了与您相同的问题。我首先尝试了“flutter upgrade”。问题仍然存在。
之后,我尝试关闭我的IDE(Android studio)并再次打开它,问题被清除了!
评论
我遇到了同样的问题,我只是关闭并再次打开我的文件夹,我正在使用 vs code。
评论
import 'package:flutter_gen/gen_l10n/app_localizations.dart'
除了@Sleepingisimportant答案之外,您可以重新启动“Dart Analysis Server”,问题将得到解决。
这个按钮位于 Android Studio 的 Dart Analysis 选项卡中,我猜这意味着它也在 Intelij 上。
评论
我遇到了同样的问题,我所做的是首先在终端中运行命令。在那之后,我再次通过命令运行颤振。成功了。flutter clean
flutter run
flutter pub get
如果这还没有完成,也可能会有所帮助。在向 pubspec.yaml 添加新内容后,这一点很重要
如果您使用的是包,则需要将其从 pubscpec.yaml 中删除以解决冲突。flutter_gen
评论
intl
flutter_gen
flutter_gen
您可能需要关闭并重新打开 IDE,以便它重新分析您的代码库。特别是 VS Code,有时无法识别对代码库的更改,但关闭并重新打开 IDE 将触发对代码的重新索引/分析,并且应该可以解决此幻影错误。
查看>命令面板,然后键入 Dart: Restart Analysis Server。
我只是在添加l10n.yaml后解决了它,然后这样做了:
- 重新启动项目
- 跑:
颤振清洁
Flutter 酒吧获取
评论
如果有人打开已添加本地化的现有项目。 这个错误总是会出现。 导入 'package:flutter_gen/gen_l10n/app_localizations.dart';不存在
因此,在终端中运行以下命令: flutter pub 添加flutter_gen
在每个 arb 文件中添加新行。例如,进入 l10n/app_en.arb。然后单击 Pub get。
请在 flutter 部分的底部添加 generate: true。然后。重新启动编辑器或 IDE 工具。运行 flutter clean 和 flutter pub get。
environment:
dependencies:
flutter:
uses-material-design: true
generate: true
如果您使用的是 VSCode, 只需单击“Shift+CMD+P”,然后单击“Dart:重新启动分析服务器”
Flutter clean
或者重新启动编辑器,Android Studio 无法解决问题。您需要在项目文件夹中拥有 l10n.yaml 文件。在创建新项目并从另一个项目复制粘贴内容后,您可能会遇到此问题。l10n.yaml 文件内部是这样的:
arb-dir: lib/src/localization
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
并将此行添加到 pubspec.yaml 中:
# Enable generation of localized Strings from arb files.
generate: true
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'.
我遇到了同样的问题,我正在使用 VSCode,我所做的只是重新启动 IDE 并且错误消失了。
我按照这里的 flutter 文档来解决这个问题:
要使用 flutter_localizations,请将包作为依赖项添加到 pubspec.yaml
文件:
dependencies:
flutter:
sdk: flutter
flutter_localizations: # Add this line
sdk: flutter # Add this line
接下来,运行 pub get packages,然后导入 flutter_localizations 库:
import 'package:flutter_localizations/flutter_localizations.dart';
下一部分对于消除错误至关重要:
添加flutter_localizations包后,请使用以下说明将本地化文本添加到应用程序。
将 intl 包添加到 pubspec.yaml
文件中:
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.17.0 # Add this line
此外,在 pubspec.yaml 文件中,启用 generate 标志。它被添加到特定于 Flutter 的 pubspec 部分,并且通常在 pubspec 文件的后面出现。
# 以下部分特定于 Flutter。
flutter:
generate: true # Add this line
在调用的 Flutter 项目的根目录下添加一个新文件,内容如下:yaml
l10n.yaml
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
此文件配置本地化工具;在此示例中,输入文件位于 ${FLUTTER_PROJECT}/lib/l10n 中,该文件提供模板,生成的本地化放在文件中。app_en.arb
app_localizations.dart
在 中,添加模板文件。${FLUTTER_PROJECT}/lib/l10n
app_en.arb
接下来,在同一目录中添加一个 app_es.arb
文件,用于同一消息的西班牙语翻译:
现在,运行您的应用程序,以便进行代码生成。您应该在以下位置看到生成的文件: ${FLUTTER_PROJECT}/.dart_tool/flutter_gen/gen_l10n.
在 app_localizations.dart
上添加 import 语句。
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
颤振清洁 Flutter 酒吧获取 Flutter Pub Run 短语 Flutter Pub 运行 intl_utils:Generate
它帮助了我,你也可以尝试一下
在这里添加我的贡献,因为没有一个答案对我有用。我浏览了文档,发现我需要运行以下命令才能生成代码。运行,然后运行flutter clean
颤振 GEN-L10N
我通过在主文件夹中的终端上运行来解决它:
颤振 GEN-L10N
对于那些从其他人那里继承该项目的人,请在任何其他试用之前运行以下内容:flutter gen-l10n
上述解决方案均无效!除了 Abdouly M. 回答的那个。 只是运行
颤振 GEN-L10N
首先 请尝试创建这样的 flutter 项目:
flutter create -t skeleton -a java -i swift --org com.meraj your_app_name
然后使用'package:flutter_gen/gen_l10n/app_localizations.dart'。 完成此过程后,如果您遇到相同的问题,请关闭编辑器并重新打开。 如果此过程不起作用,请编写命令:
flutter clean
然后写下:
flutter pub get
有时问题不在于类 AppLocalization,问题在于您的 IDE 由于某种原因无法从生成的文件夹构建合成包,您可以在 l10n 文件夹中生成代码并像手写类一样使用它。 怎么做:
- 转到您的 l10n.yaml
- 将行添加到文件中。
synthetic-package: false
这样代码将被生成到你的 lib/l10n 或任何你拥有的 arb-dir 中,IDE 不会有任何问题,这适用于每个依赖合成的代码生成的库
升级到 Flutter 3.13.1 后,它发生在我身上。
我已经有了
flutter:
generate: true
在我的位置上。
我通过从 intl 包中删除版本号来解决,因此它可以获取最新的 intl 包。
flutter packages get
然后等待几秒钟以应用更改。
如果这不起作用,请尝试
flutter clean
flutter run
将 “generate: true” 行添加到您的 pubspec.yaml 文件中:
# The following section is specific to Flutter.
颤振:生成:真
使用 flutter SDK 3.13.9。
flutter gen-l10n 应该在 flutter pub get 之后完成
在 flutterpub 运行之后......显示错误
我尝试在调试中运行 apk,但错误已从文件中删除
评论
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.