skip to Main Content

I have a cubit that looks like this:

@injectable
class SomeCubit extends Cubit<SomeState> {
  SomeCubit({
    required this.logger,
    required this.someRepository,
    required this.flutterLocalNotificationsPlugin,
    required this.l10n, // AppLocalizations
    required this.anotherRepository,
  }) : super(SomeInitial());
}

Running the injectable generator throws an error about InvalidType because AppLocalizations isn’t set up, but nothing I try allows AppLocalization to be injected.

I’ve tried doing it under the AppModule using @factoryParam, but it fails to generate code and fails out early. I’ve also tried applying the @factoryParam attribute in the SomeCubit constructor, but the same issue comes up.

Is anyone aware of how to do this and pass the current context in when attempting to get the SomeCubit dependency? I feel like there is a way to get this to work, but I’m missing something.

To summarize, I would like to use injectable with AppLocalizations.

2

Answers


  1. Chosen as BEST ANSWER

    So the issue is with injectable when it tries to generate the config.dart file.

    import 'package:flutter_gen/gen_l10n/app_localizations.dart'; is not an import that flutter will automatically add and it has to be pasted in manually all over the place. Since injectable couldn't automatically add this import it resulted in the generated file using InvalidType as a stand in which obviously broke the generated file.

    I came up with two options that worked.

    1. I removed the AppLocalizations parameter and instead passed in the BuildContext using @factoryParam. This worked, but I was worried about the context becoming outdated. Example:
    @injectable
    class SomeCubit extends Cubit<SomeState> {
      SomeCubit({
        required this.logger,
        required this.someRepository,
        required this.flutterLocalNotificationsPlugin,
        @factoryParam required this.buildContext, // BuildContext
        required this.anotherRepository,
      }) : super(SomeInitial());
    }
    
    1. I only used AppLocalizations in one method that was called when a button is pressed. I changed the method to take BuildContext context and then call AppLocalizations.of(context) from within that method. I went with this since the context would always be up to date and correct.

  2. Do you define your package’s localizations delegates in the app’s MaterialApp?

    like so:

    import 'package:your_package/l10n/app_localizations.dart' as your_package;
    import 'package:flutter/material.dart';
    import 'package:your_app/core/app_router.dart';
    
    class App extends StatelessWidget {
      const App({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp.router( // example from app using GoRouter..
          title: "Your app",
          localizationsDelegates: _localizationsDelegates, // set loca delegates here
          supportedLocales: _supportedLocales, // set supported locales here
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          routerConfig: appRouter,
        );
      }
    
      Iterable<LocalizationsDelegate<dynamic>>? get _localizationsDelegates => [
            AppLocalizations.localizationsDelegates, // your app's loca delegates
            ...your_package.AppLocalizations.localizationsDelegates, // your package's loca delegates
          ];
    
      Iterable<Locale> get _supportedLocales => [
            AppLocalizations.supportedLocales, // your_package's locales need to include the app ones
          ];
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search