How to get AppLocalizations import suggestion in Android Studio Quick Fix menu
Asked Answered
I

2

10

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

Isolda answered 31/1, 2022 at 22:7 Comment(0)
S
10

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.

Spall answered 30/8, 2022 at 9:39 Comment(0)
W
5

Solution 1: Export it once

add a file lib/localization.dart

and export localization

export 'package:flutter_gen/gen_l10n/home_localizations.dart';

Solution 2: do not use synthetic package

You do not have to have a synthetic package, you can have your generated class in your lib folder or any other folder for that matter.

Example:

Add l10n.yaml at the root of the package / app.

arb-dir: lib/localization
output-dir: lib/localization/generated
output-localization-file: account_localizations.dart
output-class: AccountLocalizations
template-arb-file: account_en.arb
synthetic-package: false 
use-deferred-loading: true
nullable-getter: false

Weitman answered 26/5, 2023 at 0:18 Comment(1)
This is fixing the problem where for some reason flutter can't see the generated files in the dart folder. I was workign yesterday and now it just don't want to see them. So problem fix and the cherry on top is not I get the import in the suggestion.Argyle

© 2022 - 2025 — McMap. All rights reserved.