Undefined name 'AppLocalizations'. (Documentation) after creating a new project

Viewed 2551

I created a new Flutter project and did a

flutter pub get

There are no errors from the framework except the one from AppLocalizations class.

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

enter image description here

Another error (obviously because of the same package)

enter image description here


NOTE:

I have already tried:

  • Restarting IDE + Invalidate Caches and Restart
  • flutter pub add flutter_gen
  • Restarting Dart Analysis Server
  • Updating packages

I'm using: Flutter 2.6.0-11.0.pre • channel dev

3 Answers

You can expect all sorts of surprises when switching between channels (and specifically when using Dev or Master channel).

If switching to stable or beta channel (flutter channel stable and flutter upgrade) due to some reasons is not an option I'd suggest upgrading flutter (newer version might have a fix) cleaning the proj OR recreatign platform projects:

   flutter upgrade 
   flutter clean
   flutter create .

Besides, I'd stick to beta channel rather than dev/master - it's typically better tested and has fewer issues.

Note that in 2022 dev channel is no longer available leaving master as the most recent and least tested branch.

flutter_gen/gen_l10n dir is auto generated by flutter and located in .dart_tool. (It's not related to the package named flutter_gen in pub.dev).

Try this,

flutter clean

then,

flutter pub get

If the flutter_gen/gen_l10n dir is not available yet follow the below steps.

Step 0: Make sure you have these in pubspec.yaml,

flutter:
  generate: true

and

dependencies:
  flutter_localizations:
      sdk: flutter

Step 1: Comment everything related to AppLocalization if you can't run the app with them. (Imports & Usage)

Step 2: Now run the app.

Now the flutter_gen/gen_l10n folder will be generated.

Finally: Un-comment everything related to AppLocalization.

Every time you make changes to .arb files you will need to flutter get / run / hot reload / hot restart the app to update generated localization files.

Change these lines in pubspec.yaml file

From

flutter:
  uses-material-design: true

To

flutter:
  uses-material-design: true

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

Then run:

flutter clean
flutter pub get
flutter run
Related