How to avoid multiple instances of a service in Angular Dart

Issue

It seems that each component that creates its own instance of [a] service. I don’t understand why.

I note this AngularJs 2 – multiple instance of service created, but I’m not clear on the correct solution. Do I create the service instance _myService in main:

 void main() {
  bootstrap(AppComponent,[MyService]);
 }

and then copy it to [child] components (because I also remove MyService from the component providers)? This doesn’t seem correct, because the components reference _myService before it’s instantiated, and I have to check it for being null.

Thanks

Steve

Solution

You can use factory constructor pattern like here.

Answered By – biv

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *