Comments (11)
I typically have a file backend.dart
like this here
import 'package:aartos/services/rtsa_service_.dart';
import 'package:aartos/services/rtsa_service_impl.dart';
import 'package:get_it/get_it.dart';
export 'package:aartos/services/rtsa_service_.dart';
GetIt backend = GetIt();
void initBackend()
{
backend.registerSingleton<RTSAService>(RTSAServiceImplementation());
backend<RTSAService>().init('192.168.178.91', 54664);
//backend<RTSAService>().init('clabuster.dyndns.org', 54664);
}
And I call initbackend from main
before runApp
you can do the same from your tests
Also don't over engineer its an App. A global is perfectly fine
from get_it.
@Daghis What would you gain? You would still need to import the filer where the GetIt class is defined.
from get_it.
from get_it.
Same question here, @cosinus84 are you sure this is the best practice?
from get_it.
You only declare on global viable for the servicelocator and use it everywhere
from get_it.
I just started looking at using this to handle some of my dependency management. It appears that it'll make it much easier to mock services used by the code being tested. (Note that I'm new to Dart and Flutter, so I'm speaking with minimal experience in that world.)
That said, I have a question about the need to use a global variable. Do you think it would be appropriate instead to have a shared instance that could then be referenced directly from the class as a static method? For example, one could have GetIt.getInstance().get<Firestore>()
which would have the same effect without requiring importing a Dart file just to get access to its global variable.
Also, if one does this, one could have a private class field as a short cut like GetIt _getIt = GetIt.getInstance();
.
I'm not sure that this is necessarily a better practice than what's recommended, but it came to mind while starting to use this package.
from get_it.
That is a very valid question to which I'm not sure I have a good answer. Part of that is a lack of understanding of how things are done best in Dart (coming from a Java background).
In my limited experience, I had defined the getIt global variable in my lib/main.dart, so it seemed undesirable for my test files to import that (although it doesn't seem to cause any actual problems). Now, the obvious solution would be to create a separate Dart file that contains any such commonly used global declarations that could be included by lib/main.dart as well as the test files.
Now, this is one of the places where my Dart experience is significantly lacking. With Java, I believe that if I want to access a GetIt method, not only would I have to import the file defining the global variable, I'd also have to import the GetIt class to get the method definitions. It seems that with Dart, that part isn't necessary. In that case, I'm really not sure there is any functional advantage, although perhaps there might be a stylistic one. Again, though, that comes from a non-Dart perspective.
from get_it.
That sounds perfectly fine! Thank you very much for your help and input.
from get_it.
I have just spent the weekend implementing Get It into my project and I couldn't be happier with what it has done for my code structure. I've written a bunch of widget tests and they all work well...in isolation. When I run all of the tests, a few of them start interfering with each other (because everything is using the same singleton instances) - I cannot think of a way in which to isolate them from each other (for example, one is a mock of a data store and I want to test different combinations of data going back to my app for the same calls).
EDIT: This wasn't really a get_it issue, but rather what was happening was that I wasn't cleaning up after my own tests. Leaving the question and resolution here for anyone who comes across this and wants to blame the wrong thing.
from get_it.
@escamoteur can you close this if there is no action or changes required from the package side.
i hope best practiced are already included in Readme file
from get_it.
from get_it.
Related Issues (20)
- Add new type of scope that is independ of the scope stack HOT 1
- Build error in version 7.6.1 HOT 7
- unlisted from pub.dev? HOT 4
- Null check operator used on a null value with allReady on hot refresh HOT 2
- Can't register AutoRoute v6 with GetIt HOT 2
- Bad state: GetIt: Object/factory with type AdviceRepoImpl is not registered inside GetIt. HOT 1
- Check implementation of `WillSignalReady` on subclasses HOT 1
- Collections Multibinding HOT 5
- Questions about the registerFactory HOT 2
- Error on example code for addListener HOT 10
- Concerns about Get_It HOT 4
- Need Help: Dependency Problem with GetIt in vm:entry-point Pragma HOT 1
- `unregister<T>` unregisters all services of type `T` HOT 9
- Webview shows blank untill the app is sent to background and brought forward again ... HOT 1
- trying to upgarde the package to 7.6.9 HOT 9
- Make pushing a new scope atomic. HOT 3
- Get it problem HOT 1
- [feature request] `registerCachedInstance`? HOT 14
- Race condition in popScopesTill HOT 12
- Bad state: GetIt: Object/factory with type <?> is not registered inside GetIt HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from get_it.