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

Viewed 28084

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.
22 Answers

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

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

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

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

  1. restart your project
  2. Run:

flutter clean

flutter pub get

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.

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

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.

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

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.

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';

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

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.

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

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

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

Dart: Restart Analysis Server

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

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

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.

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

It helped me, you can try it too

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

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

flutter gen-l10n

Related