Problem Statement
Currently, bringing up the Android Studio Quick Fix menu (Opt/Alt+Enter keyboard shortcut) on AppLocalizations does not suggest importing the generated file.
The AppLocalizations class lives at .dart_tool/flutter_gen/gen_l10n/app_localizations.dart
, which is also a Git-ignored directory.
As a result I have to type the import statement by hand. Other Flutter class names commonly suggest importing a relevant file via the Quick Fix menu.
What Is Expected
I am expecting the Quick Fix menu to suggest importing the generated AppLocalizations file. When I click on the import suggestion, it should insert it alongside my other import statements at the top of the file.
Question
How can I get the import suggestion to appear in the Quick Fix menu for AppLocalizations? Do I need to help the Dart analyzer "know about" the generated files inside the .dart_tool
directory? Can I include the generated file in my "Project" while still Git-ignoring it? Do I somehow need to link to it in my pubspec file?
2
Answers
Flutter needs to generate the file with code from json file, you can do it by running e.g.
flutter pub get
orflutter pub upgrade
orflutter build ...
orflutter run
I didn’t find a solution for an automatic suggestion.
Just add this import manually. Note: it still at first didn’t recognise
AppLocalizations.delegate
, so I removed this line of code and started typing again. Now it worked.EDIT:
Since you need to import
AppLocalizations
at a lot of places, for me the workaround was to create a class that gets me an instance of this class.I have created a
common.dart
class with a function:And I call translations from my widgets like this:
This way you add an import manually only at one place.