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

34

92

I am now using flutter gallary in my project, this is the package reference:

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

but it shows:

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

I added lib in pubspec.yaml:

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

and added 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

Am I missing something? still not work, what should I do to make it work? This is the full code:

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,
            ),
          ),
        ),
      ],
    );
  }
}

when I run the command flutter clean && flutter run , shows the result:

[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.
Employer answered 28/10, 2020 at 14:11 Comment(2)
Do flutter clean && flutter run. It will work, I presumeTejada
[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.Employer
D
5

I got the imports working by adding these two lines to the pubspec.yaml file:

cupertino_icons: ^0.1.3 
flutter_gallery: ^2.4.0+20400

The first line is actually replacing the original cupertino-icons dependency that is version 1.0.0 and the flutter gallery dependency requires a different version, that is 0.1.3.

Then update the imports with 'flutter pub get'

This site here lists all of the possible flutter_gallery imports.

Daveen answered 4/12, 2020 at 13:1 Comment(1)
adding intl: ^0.17.0 to dependencies solved it for meTindal
P
101

I followed the Flutter official doc (https://flutter.dev/docs/development/accessibility-and-localization/internationalization) but came across the same problem as you. I first tried "flutter upgrade". The issue still remained.

After that, I tried to close my IDE(Android studio) and open it again, and the issue was cleared!

Pumice answered 16/4, 2021 at 12:12 Comment(1)
I thought such problems can only be found in eclipse and not in intellij... but here we go... a restart helped.Parhelion
B
73

Additionally to @Sleepingisimportant answer's, you can restart "Dart Analysis Server" and the issue will be resolved.

Restart Dart Analysis Server

This button is in the Dart Analysis Tab here on Android Studio which I guess means it's also on Intelij.

enter image description here

Buddle answered 10/8, 2021 at 17:48 Comment(2)
Thank you!! The file is there, but the Dart daemon is not clever enough to find it. Closing IDE and reopening works sometimes, this seem to work always.Taxation
This should be the accepted answer.Carolecarolee
M
51

If you are using a package flutter_gen you need to remove it from pubscpec.yaml to resolve conflict.

Multivocal answered 2/11, 2021 at 6:39 Comment(9)
that's it ThanksBillington
holy cow this was it....Triumvir
That was my issue also. Basically the intl package generates localisation code under a fake flutter_gen package, but there is also a real flutter_gen package on pub.dev. If you install the latter, it will take priority and hide the generated localisation code, so it will appear to not exist.Overlarge
oh my friend thank you so much you are life savior. thanks a lotSidsida
I had the same problem: just try to remove 'generate: true' in your pubspec.yaml and disable 'synthetic-package: false' in your l10n.yamlLowland
that's it, after 30 mins try to restart my IDE.Incognizant
thanks that helped, I created my app with community suggestions and ran dart fix --apply which seems to have broken it. Removing flutter_gen worked.Engaged
Crazy thing is I don't even recall why / how flutter_gen was added to my pubspecSalad
Big thanks! Removing this package and keeping intl only worked!Coniferous
J
37

I just solved it after adding l10n.yaml, then did this:

  1. restart your project
  2. Run:

flutter clean

flutter pub get

Jeanejeanelle answered 13/2, 2022 at 13:37 Comment(4)
man, that's helped me, better to do it first, before adding dependencies as in chosen answerKhoisan
helped me too! dont get why flutter clean resolves this but anyway...Tightwad
@Tightwad Because it's a problem with the cash memory, it need just to be refreshed, that is because there is no perfect software even if it's from Google.Jeanejeanelle
This is the only method that worked for me. Tried restarting the editor but didn't work.Cutwater
C
22

I had the same problem, I just closed and opened my folder again, I'm using vs code.

Cordwainer answered 27/6, 2021 at 10:56 Comment(2)
I had this same issue with import 'package:flutter_gen/gen_l10n/app_localizations.dart' in Visual Studio Code, and after closing the window and reopening it, the error was gone.Bedight
Does not work if you do not have l10n.yaml file inside project folder and generate: true inside pubscpec.yaml. It just wastes time and all errors show up again after editor finishes indexing project files.Myxomycete
P
22

View > Command Palette and then typing Dart: Restart Analysis Server.

Philpot answered 11/1, 2022 at 4:48 Comment(0)
J
16

I solved it by running on the terminal in the main folder:

flutter gen-l10n
Jeffcott answered 3/9, 2022 at 10:14 Comment(0)
P
9

I faced the same issue and what I did was I ran flutter clean command first in the terminal. After that I ran the flutter by flutter run command again. It worked.

Paleobiology answered 8/9, 2021 at 8:59 Comment(0)
F
8

Try to run flutter update-packages in ~/flutter/packages/flutter.

flutter update-packages

Or update the Flutter SDK by using the flutter upgrade command:

flutter upgrade

This command gets the most recent version of the Flutter SDK that’s available on your current Flutter channel.

More information on how to upgrad the Flutter SDK or switching Flutter channels : https://flutter.dev/docs/development/tools/sdk/upgrading

This will fix your import 'package:flutter_gen/gen_l10n/gallery_localizations.dart'; issue.

Furmenty answered 16/11, 2020 at 9:39 Comment(0)
P
7

At the bottom of our pubspec.yaml you should set generate to 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
Polypropylene answered 12/11, 2020 at 18:32 Comment(1)
This was the solution for me :) Thanks.Catinacation
D
5

I got the imports working by adding these two lines to the pubspec.yaml file:

cupertino_icons: ^0.1.3 
flutter_gallery: ^2.4.0+20400

The first line is actually replacing the original cupertino-icons dependency that is version 1.0.0 and the flutter gallery dependency requires a different version, that is 0.1.3.

Then update the imports with 'flutter pub get'

This site here lists all of the possible flutter_gallery imports.

Daveen answered 4/12, 2020 at 13:1 Comment(1)
adding intl: ^0.17.0 to dependencies solved it for meTindal
O
5

for those who inherit the project from some others, please run below before any other trial:

flutter gen-l10n
Omnipotence answered 19/10, 2022 at 1:24 Comment(0)
H
5

i just remove this line flutter_gen in pubspec.yaml then run flutter gen-l10n and it work

Heterogenesis answered 29/3 at 3:5 Comment(1)
In my case, this problem occurs after I run "dart fix --apply", it seems this command add a missing package "flutter_gen" to pubspec.yaml, that cause the "import 'package:flutter_gen/gen_l10n/app_localizations.dart'" doesn't work anymore. Just remove "flutter_gen" from pubspec.yaml and save, it work fine!Watteau
E
3

flutter pub get could also help if this has not already been done. This is important after adding new stuff to pubspec.yaml

Environ answered 1/11, 2021 at 21:51 Comment(0)
S
3

I followed the flutter documentations here to resolve the issue:

To use flutter_localizations, add the package as a dependency to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations: # Add this line
    sdk: flutter         # Add this line

Next, run pub get packages, then import the flutter_localizations library:

 import 'package:flutter_localizations/flutter_localizations.dart';

The next part is critical in removing the error:

Once the flutter_localizations package is added, use the following instructions to add localized text to your application.

Add the intl package to the pubspec.yaml file:

  dependencies:
      flutter:
        sdk: flutter
      flutter_localizations:
        sdk: flutter
      intl: ^0.17.0 # Add this line

Also, in the pubspec.yaml file, enable the generate flag. This is added to the section of the pubspec that is specific to Flutter, and usually comes later in the pubspec file.

# The following section is specific to Flutter.

flutter:
  generate: true # Add this line

Add a new yaml file to the root directory of the Flutter project called l10n.yaml with the following content:

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart

This file configures the localization tool; in this example, the input files are located in ${FLUTTER_PROJECT}/lib/l10n, the app_en.arb file provides the template, and the generated localizations are placed in the app_localizations.dart file.

In ${FLUTTER_PROJECT}/lib/l10n, add the app_en.arb template file.

Next, add an app_es.arb file in the same directory for Spanish translation of the same message:

Now, run your app so that codegen takes place. You should see generated files in: ${FLUTTER_PROJECT}/.dart_tool/flutter_gen/gen_l10n.

Add the import statement on app_localizations.dart.

import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Savoury answered 21/7, 2022 at 11:40 Comment(0)
S
3

Adding my contribution here, as none of the answers worked for me. I went through the documentation and found that I need to run the following command for code generation to take place. Run flutter clean and then run

flutter gen-l10n
Syverson answered 15/8, 2022 at 6:36 Comment(1)
worked for me!!Gareth
H
3

Just remove flutter_gen from your pubspec.yaml if exist and check generate: true, if not fixed reload the project and follow this instructions:

https://docs.flutter.dev/ui/accessibility-and-internationalization/internationalization

Healthy answered 1/4 at 10:11 Comment(0)
T
3

current: Flutter 3.22.0, I just always run:

dart run build_runner build -d && flutter pub get

flutter pub get - is that create flutter_gen package, and build_runner always destroy it((

Tub answered 21/5 at 7:54 Comment(0)
M
2

If you'r using VSCode, simply click "Shift+CMD+P" then "Dart: Restart Analysis Server"

Dart: Restart Analysis Server

Matisse answered 15/5, 2022 at 18:21 Comment(0)
B
2

I had the same problem with a project running the latest 3.22.0 version of flutter. I tried everything written here but nothing worked. I had the problem in my CI environment where everything is cleanly installed (no IDE to restart).

I found out that the folder .dart_tools had code generated after executing flutter gen-l10n. So the problem was that dart just couldn't see it. After executing flutter pub get again the generated code was available to the project. So I needed to update my CI script like:

  - flutter pub get
  - flutter gen-l10n
  - dart run build_runner build
  # RUN PUB GET AGAIN TO RESCAN THE .dart_tool folder
  - flutter pub get

I hope it helps.

Babysitter answered 20/5 at 9:25 Comment(0)
A
1

You might need to close and reopen your IDE so it re-analyses your code base. VS Code in particular sometimes doesn't recognise changes to the code base but closing and reopening your IDE will trigger a re-index / analysis of your code and should resolve this phantom error.

Aide answered 4/1, 2022 at 12:46 Comment(0)
F
1

Add new line at every arb files. E.g. into l10n/app_en.arb. Then click Pub get.

Farrica answered 25/3, 2022 at 9:35 Comment(0)
H
1

Please add generate: true in the bottom of flutter section. After that. Restart your editor or IDE tool. Run flutter clean and flutter pub get.

environment:
dependencies:
flutter:
  uses-material-design: true
  generate: true
Hooker answered 16/4, 2022 at 6:12 Comment(0)
C
1

First of all, Please try to create flutter project like this :

flutter create -t skeleton -a java -i swift --org com.meraj your_app_name

Then use 'package:flutter_gen/gen_l10n/app_localizations.dart'. After this process, if you face the same problem then close your editor and open again. If this process don't work, then write a command:

flutter clean 

And then write:

flutter pub get
Cioffred answered 22/12, 2022 at 8:53 Comment(0)
E
1

sometimes the issue is not about class AppLocalization, the issue is that your IDE can't build synthetic packages from generated folders for some reason, you can generate code inside your l10n folder and use it like a handwritten class. How to do that:

  1. go to your l10n.yaml
  2. add synthetic-package: false line into file.

This way code will be generated into your lib/l10n or whatever you have as arb-dir and IDE will not have any issues, this goes with every code generated lib that rely on synthetics

Emeraldemerge answered 26/4, 2023 at 16:38 Comment(0)
H
0

If someone open existing project with Localisation already added. This error always comes. import 'package:flutter_gen/gen_l10n/app_localizations.dart'; not exist

So run this command in terminal : flutter pub add flutter_gen

Hairbrush answered 24/2, 2022 at 8:50 Comment(0)
M
0

Flutter clean or restarting editor, Android Studio won't solve the issue. You need to have l10n.yaml file inside project folder. You probably faced with this issue after creating new project and copy-pasting content from another project. Inside of l10n.yaml file is like this:

arb-dir: lib/src/localization
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart

And add this line into pubspec.yaml:

  # Enable generation of localized Strings from arb files.
  generate: true

More details here

Myxomycete answered 6/7, 2022 at 12:16 Comment(0)
M
0
Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart'.

I've had the same issue, and I'm using VSCode, all I did was restart the IDE and the error gone.

Moorwort answered 15/7, 2022 at 14:7 Comment(0)
D
0

flutter clean flutter pub get flutter pub run phrase flutter pub run intl_utils:generate

It helped me, you can try it too

Delaware answered 4/8, 2022 at 14:51 Comment(0)
J
0

It happened to me after upgrading to Flutter 3.13.1.

I already had

flutter:
  generate: true

in my place.

I solved by removing the version number from intl package, so it can get the latest intl package.

Jacquelinjacqueline answered 26/8, 2023 at 9:2 Comment(0)
L
0
 flutter packages get

Then wait some seconds to apply the changes.

If that does not work, try

 flutter clean
 flutter run
Lizethlizette answered 31/8, 2023 at 10:15 Comment(0)
N
0

Add "generate: true" line to your pubspec.yaml file :

# The following section is specific to Flutter.

flutter: generate: true

Nevlin answered 1/9, 2023 at 9:6 Comment(0)
R
0
  1. Add the following to your pubspec.yml
dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: any
  1. In the pubspec.yaml file enable the generate flag
flutter:
  generate: true # Add this line
  1. Create a l10n.yaml file at the root of your project include following content :
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
nullable-getter: false # Optional line

Adapt the code above to your project and naming

  1. Run flutter clean && flutter pub get
Rye answered 20/1 at 12:41 Comment(0)
L
0

You can do:

  1. Close and reopen the IDE (and the project)

Sometimes the error is corrected this way. If the error stills, in the terminal with the project do:

  1. flutter clean && flutter run
  2. flutter pub get (or press pub get in Android Studio)

If still not working: Click on Restart Data Analysis Server

If still not working:

  • flutter gen-l10n

If you didn't have l10n.yaml, create it like this:

arb-dir: lib/l10n                     # files folder
template-arb-file: app_en.arb         # default template
output-localization-file: app_localizations.dart  # generation localization file
untranslated-messages-file: untranslated_messages.txt

If you didn't have any translation, follow the documentation

Leniency answered 21/4 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.