skip to Main Content

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?

Screenshot of Quick Fix menu missing import suggestion

screenshot of Quick Fix menu not suggesting import with AppLocalizations

2

Answers


  1. Flutter needs to generate the file with code from json file, you can do it by running e.g. flutter pub get or flutter pub upgrade or flutter build ... or flutter run

    Login or Signup to reply.
  2. I didn’t find a solution for an automatic suggestion.

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

    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:

    import 'package:flutter/material.dart';
    import 'package:flutter_gen/gen_l10n/app_localizations.dart';
    
    
    
    AppLocalizations? getApplocalizations(BuildContext context){
      return AppLocalizations.of(context);
    }
    
    

    And I call translations from my widgets like this:

    title: Text(getApplocalizations(context)?.pageHomeTitle("Test") ?? ""),
    

    This way you add an import manually only at one place.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search