GetIt注入报错:NotificationRepositoryImpl依赖未注册的NotificationService
我了解已有类似问题,但未找到解决方案。NotificationRepository依赖NotificationService,而后者依赖Flutter Local Notifications插件实例。
初始化代码
// Initialization final GetIt getIt = GetIt.instance; @InjectableInit( initializerName: 'init', // default preferRelativeImports: true, // default asExtension: true, ) void configureDependencies() => getIt.init();
已在main函数中调用该方法。
模块代码
@module abstract class InjectableModule { // for other use @lazySingleton Dio dio() => ... @lazySingleton FlutterLocalNotificationsPlugin getLocalNotification() => FlutterLocalNotificationsPlugin(); }
这是用于将FlutterLocalNotificationsPlugin注入NotificationService的模块。
NotificationService实现
abstract class NotificationService { // required functions... } @LazySingleton(as: NotificationService) class NotificationServiceImp implements NotificationService { final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; NotificationServiceImp(this.flutterLocalNotificationsPlugin); ..... }
NotificationRepository实现
abstract class NotificationRepository { .... } @LazySingleton(as: NotificationRepository) class NotificationRepositoryImpl implements NotificationRepository { final NotificationService _notificationService; NotificationRepositoryImpl(this._notificationService); .... }
运行build runner时的警告
[WARNING] injectable_generator:injectable_config_builder on lib/di/get_it.dart:
Missing dependencies in statedemo/di/get_it.dart[NotificationRepositoryImpl] depends on unregistered type [NotificationService] from package:statedemo/data/services/notification_serivce_imp.dart
运行代码时的错误
Bad state: GetIt: Object/factory with type NotificationService is not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)
项目中其他依赖(如Dio)的GetIt注入正常,仅在实现通知功能时出现问题。手动实现NotificationService单例模式时功能正常,尝试命名实例后仍报错:
Bad state: GetIt: Object/factory with with name NotificationServiceImp and type NotificationService
is not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)
1. 确保注解被正确扫描
在configureDependencies所在文件顶部添加文件级别的@Injectable()注解,或者在@InjectableInit中明确指定扫描目录,避免injectable遗漏目标文件:
@Injectable() final GetIt getIt = GetIt.instance; @InjectableInit( initializerName: 'init', preferRelativeImports: true, asExtension: true, generateForDir: ['lib/data/services', 'lib/data/repositories'], ) void configureDependencies() => getIt.init();
2. 修正拼写错误
警告中提到的文件路径是notification_serivce_imp.dart,其中serivce是拼写错误(正确应为service)。务必修正文件名、类名及所有相关导入路径的拼写,保证完全一致——拼写错误会导致注解扫描失效。
3. 清理并重新生成代码
删除旧的生成文件(通常是lib/di/get_it.config.dart),然后执行以下命令重新生成:
flutter pub run build_runner clean flutter pub run build_runner build --delete-conflicting-outputs
4. 显式添加构造函数注解
如果上述步骤无效,尝试给NotificationServiceImp的构造函数添加@Injectable()注解(虽然@LazySingleton已包含该注解,但部分场景下显式添加可解决扫描问题):
@LazySingleton(as: NotificationService) class NotificationServiceImp implements NotificationService { final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; @Injectable() NotificationServiceImp(this.flutterLocalNotificationsPlugin); // ... }
内容的提问来源于stack exchange,提问作者Suraj Rao

