skip to Main Content

Background

  1. I’m using flutter_localizations to localize my app.
  2. I’m using build_runner for generating files. (riverpod, freezed etc).

pubspec.yaml

...

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  intl: 0.18.1

...

dev_dependencies:
  build_runner: ^2.4.8
  flutter_gen: ^5.4.0

...

flutter:
  generate: true

...

l10n.yaml

arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart

some_widget.dart

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

class SomeWidget extends StatelessWidget {
  const SomeWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Text(AppLocalizations.of(context).someText);
  }
}

Problem

flutter_gen is the issue. I must add package: flutter_gen to the dev dependency.

  • Why I must add this

    After implementing localization according to the official Flutter Doc, running the command dart run build_runner build causes an error (Bad state: Unable to generate package graph). This is discussed here. It was solved by adding flutter_gen.

  • Adding flutter_gen causes issues

    Files containing l10n code such as: AppLocalizations.of(context).someText raises an error: Target of URI doesn't exist: 'package:flutter_gen/gen_l10n/app_localizations.dart' on import line 'package:flutter_gen/gen_l10n/app_localizations.dart';. This is explained here. Simplified, flutter creates the l10n generated files under a fake package name under flutter_gen which conflicts with the real flutter_gen added under dev dependency.

What I tried

  • Restarting VS Code, restarting Dart analyzer.

  • flutter clean, other various deleting cache methods.

I’m looking for a solution where I can import AppLocalizations without errors and I can run build_runner commands without error.

2

Answers


  1. Chosen as BEST ANSWER

    Since generating under a fake package name flutter_gen causes the issue, it can be prevented by changing the generated file output location to your source code (under the lib/ folder).

    Change:

    l10n.yaml

    arb-dir: lib/l10n
    template-arb-file: app_en.arb
    output-localization-file: app_localizations.g.dart # <-- The location where it's generated. Doesn't have to be a `.g.dart`.
    output-dir: lib/l10n/generated
    synthetic-package: false # <-- We won't reference l10n stuff by the fake package name anymore.
    

    some_widget.dart

    import 'package:flutter/material.dart';
    import 'package:<project_name>/l10n/generated/app_localizations.g.dart';
    
    class SomeWidget extends StatelessWidget {
      const SomeWidget({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Text(AppLocalizations.of(context).someText);
      }
    }
    

  2. Try running this command if the above answer doesn’t work for you.

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