diff --git a/injectable/lib/src/injectable_annotations.dart b/injectable/lib/src/injectable_annotations.dart index f9dfbd7..3fc89cc 100644 --- a/injectable/lib/src/injectable_annotations.dart +++ b/injectable/lib/src/injectable_annotations.dart @@ -59,6 +59,11 @@ class InjectableInit { /// defaults to false final bool throwOnMissingDependencies; + /// throw an error and abort generation + /// in case of duplicate dependencies + /// defaults to true + final bool throwOnDuplicateDependencies; + /// a List of external package modules to be registered /// in the default package initializer before root dependencies /// classes passed here must extend [MicroPackageModule] @@ -89,6 +94,7 @@ class InjectableInit { this.asExtension = true, this.usesNullSafety = true, this.throwOnMissingDependencies = false, + this.throwOnDuplicateDependencies = true, this.includeMicroPackages = true, this.externalPackageModulesAfter, this.externalPackageModulesBefore, @@ -104,6 +110,7 @@ class InjectableInit { this.externalPackageModulesBefore, this.usesConstructorCallback = false, this.throwOnMissingDependencies = false, + this.throwOnDuplicateDependencies = true, this.ignoreUnregisteredTypesInPackages = const [], this.usesNullSafety = true, }) : _isMicroPackage = true, diff --git a/injectable_generator/lib/generators/injectable_config_generator.dart b/injectable_generator/lib/generators/injectable_config_generator.dart index 9070c24..bc72aca 100644 --- a/injectable_generator/lib/generators/injectable_config_generator.dart +++ b/injectable_generator/lib/generators/injectable_config_generator.dart @@ -35,6 +35,8 @@ class InjectableConfigGenerator extends GeneratorForAnnotation { annotation.read('usesConstructorCallback').boolValue; final throwOnMissingDependencies = annotation.read('throwOnMissingDependencies').boolValue; + final throwOnDuplicateDependencies = + annotation.read('throwOnDuplicateDependencies').boolValue; final targetFile = element.source?.uri; final preferRelativeImports = annotation.read("preferRelativeImports").boolValue; @@ -141,7 +143,10 @@ class InjectableConfigGenerator extends GeneratorForAnnotation { targetFile, throwOnMissingDependencies, ); - _validateDuplicateDependencies(deps); + + if (throwOnDuplicateDependencies) { + _validateDuplicateDependencies(deps); + } /// don't allow registering of the same dependency with both async and sync factories final groupedByType = deps.groupListsBy((d) => (d.type, d.instanceName));