9

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

chemturion
  • 283
  • 2
  • 16

2 Answers2

8

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.

Diana
  • 935
  • 10
  • 31
1

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

Ced
  • 15,847
  • 14
  • 87
  • 146