injectable icon indicating copy to clipboard operation
injectable copied to clipboard

Circular Dependency

Open venkata-reddy-dev opened this issue 3 years ago • 1 comments

Thanks for this great package.

Below are the dependenceis

@singleton
class FooA {
  FooC fooC;
  FooA(this.fooC);
}
@singleton
class FooB {
  FooC fooC;
  FooB(this.fooC);
}
@singleton
class FooC {
  FooA fooA;
  FooB fooB;
  FooC(this.fooA, this.fooB);
}

Error : when running flutter packages pub run build_runner build --delete-conflicting-outputs

[INFO] Generating build script...
[INFO] Generating build script completed, took 481ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 107ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 790ms

[INFO] Running build...
[INFO] 1.1s elapsed, 0/16 actions completed.
[INFO] 2.1s elapsed, 0/16 actions completed.
[INFO] 3.2s elapsed, 0/16 actions completed.
[INFO] 11.5s elapsed, 0/16 actions completed.
[INFO] 12.7s elapsed, 7/23 actions completed.
[SEVERE] injectable_generator:injectable_config_builder on lib/src/config/dependency_config.dart:

Stack Overflow
[INFO] Running build completed, took 36.4s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 51ms

[SEVERE] Failed after 36.4s
pub finished with exit code 1

I came from Spring / Spring boot background. is there any posibility to add setter injection to reslove this issue

venkata-reddy-dev avatar Jan 09 '22 08:01 venkata-reddy-dev

I havent notice possibility to inject by setter.

But I guess you can solve your problem by little hack like this:

@injectable
class A {
  B? _b;
  void set b(B b) => _b = b;
  
  someMethodIWantToCallInFuture() {
    _b?.cookMeLunch();
  }
}
  
@injectable
class B {
  final A a;
  B ({ required this.a }) {
    a.b = this;
  }
  
  cookMeLunch() {
    print('lunch cooked');
  }
}

Huholoman avatar Feb 07 '22 23:02 Huholoman

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 Mar 16 '23 08:03 github-actions[bot]