3

I do want to use the packages get_it and injectable. They are both wonderful, but I'm facing a issue. Maybe my idea is wrong.

My application has two singletons and I want to use different base urls for different app flavors. So I added a parameter to the singleton constructor with the baseUrl value.

The problem is now, that injectable has only @factoryParam as annotation. The name shows the problem. It's not for singletons.

What will be the best practice for that issue? Should I get rid of constructor injection and inject the baseUrl within the singleton like

var url = get_it.get<Config>();

or is that not the best idea?

1
  • 1
    Let me know if you've found the solution, I'm stuck at the same issue :( Commented Sep 16, 2020 at 13:52

1 Answer 1

3

It can be achievied in few ways, you can register different strings for different env e.g.

@Environment("dev")
@Named("url") // in case if you injecting other strings
String get devUrl...

@Environment("stage")
@Named("url") // in case if you injecting other strings
String get stageUrl...

[Update with example]

@Environment("dev")
@injectable
class ExampleOne{
final String _url;

ExampleOne(@Named("url") this._url)
}

@Environment("stage")
@injectable
class ExampleTwo{
final String _url;

ExampleTwo(@Named("url") this._url);
}

or just one class:

@injectable
class Example{
final String _url;
Example(@Named("url") this._url);
}
Sign up to request clarification or add additional context in comments.

1 Comment

And how can I use get_it to retrieve the "url"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.