injectable icon indicating copy to clipboard operation
injectable copied to clipboard

Renaming module causes dependency injection to fail in the runtime

Open gabrysgab opened this issue 1 year ago • 8 comments

image

When i'm using name FirebaseCrashlyticsLoggerModule for the module everything works fine. When i rename it to CrashlyticsLoggerModule dependency injection fails in the runtime with message

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: GetIt: Object/factory with type LoggerToolsProvider is not registered inside GetIt. 
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;

LoggerToolsProvider depends on the module. One thing that is changing is the module class name.

gabrysgab avatar Oct 02 '23 10:10 gabrysgab

@gabrysgab is there a chance there might be more then one class in your project named that?

Milad-Akarie avatar Oct 05 '23 10:10 Milad-Akarie

Nope. Only this one module is named that way.

gabrysgab avatar Oct 05 '23 10:10 gabrysgab

@gabrysgab what role does LoggerToolsProvider play in this?

Milad-Akarie avatar Oct 05 '23 11:10 Milad-Akarie

Module provides firebase crashlytics instance to :

@injectable
class FirebaseCrashlyticsLoggerTool extends LoggerTool {
  final FirebaseCrashlytics _firebaseCrashlytics;

  FirebaseCrashlyticsLoggerTool(this._firebaseCrashlytics);

  @override
  void addLog(String msg, LogType type, String? name, {Object? e, StackTrace? st}) {
    if (type != LogType.debug) {
      final logName = name != null ? "[$name] " : "";

      var message = "$logName[${type.tag}] $msg";

      _firebaseCrashlytics.log(message);

      if (type == LogType.error) {
        _firebaseCrashlytics.recordError(e ?? msg, st);
      }
    }
  }
}

and then it is injected to LoggerToolsProvider

@LazySingleton(as: LoggerToolsProvider)
class LoggerToolsProviderImpl extends LoggerToolsProvider {
  final FirebaseCrashlyticsLoggerTool _firebaseCrashlyticsLoggerTool;
  final FirestoreLoggerTool _firestoreLoggerTool;

  LoggerToolsProviderImpl(
    this._firebaseCrashlyticsLoggerTool,
    this._firestoreLoggerTool,
  );

  @override
  List<LoggerTool> get tools => [
        _firebaseCrashlyticsLoggerTool,
        _firestoreLoggerTool,
      ];
}

gabrysgab avatar Oct 05 '23 11:10 gabrysgab

@gabrysgab that is weird, initializing get_it is not throwing any errors?

Milad-Akarie avatar Oct 05 '23 11:10 Milad-Akarie

Just getting that one error that is in my first message.

gabrysgab avatar Oct 05 '23 11:10 gabrysgab

@Milad-Akarie we also have multi module architecture. You think that this may cause some issues?

gabrysgab avatar Oct 18 '23 06:10 gabrysgab

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions

github-actions[bot] avatar Nov 17 '23 08:11 github-actions[bot]