Comments (8)
Where do you call your register function? If you do it directly in main before runApp it should not happen.
from get_it.
Sorry, I haven't had noticed you have answered, but yes, that solved my problem. Thanks!
from get_it.
Did this solve your problem?
from get_it.
sorry guys, i am trying to register with context in this case. If i do hot reload getting this error. I need context to register, so i did inside the
build(BuildCotext context){
setsetupLocator(context)
}
so how to do in this case. Thanks
from get_it.
@MuthuHere this is possible, but you'll need some logic to handle it inside your GetIt setup code. Here's an example:
void setUpServiceLocator(BuildContext context) {
if (getIt.isRegistered<AppLocalizations>()) {//setup was called before, we only re-register what depends on context
getIt.unregister<AppLocalizations>();
getIt.registerSingleton<AppLocalizations>(AppLocalizations.of(context));
return;
}
getIt.registerSingleton<AppLocalizations>(AppLocalizations.of(context));
//do all your other setup that's independent of context here
}
from get_it.
@MuthuHere don't do this inside a build function. It can be called anytime. Use a StatefulWidget and access the context in didChangeDependency
.
What do you want to register that needs a context? Maybe there is a better way to do this.
@josh-ksr don't use the .of
to reach something to register in get_it. Instead register Applocalization
before you call runApp
directly in get_it as it doesn't depend on the context.
from get_it.
@escamoteur I don't think it is possible to use the new flutter localization system without context, at least i haven't found a way to use it without context yet. But sometimes there's the need to access it independent of a view, for example when creating local notifications inside the business logic layer.
That's why i added a builder to the MaterialApp and setup GetIt inside it, seems to work without problems so far even with hot reload. Or is there any problem with this approach that i just haven't seen yet? Here's some example code:
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, widget) {
setUpServiceLocator(context);
return FutureBuilder(
future: getIt.allReady(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
return widget;
} else {
return Container(color: Colors.white);
}
});
},
);
}
from get_it.
yes, that should work fine. I was just looking at the examples here and didn't see why you need the context when creating the AppLocalisation instance.
from get_it.
Related Issues (20)
- [Feature Request] Ability to verify getIt configuration
- getIt.reset() should dispose Singletons in reverse registration order HOT 4
- [Feature] Option for ignoreReassignment HOT 5
- The license page is wrong with certain fonts. HOT 3
- Null check operator used on a null value. Error thrown Instance of 'ErrorDescription'. HOT 3
- Make a lazy `registerSingletonWithDependencies` HOT 5
- Make `registerLazySingletonAsync` have a `dependsOn` HOT 7
- Async factorties should have a `dependsOn` HOT 20
- `registerLazySingletonAsync` not working with `dependsOn` HOT 3
- 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
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.