Comments (10)
sure, that is expected. getAsync returns a Future so you have to write it this way:
(await GetIt.I.getAsync<IAppModel>()).addListener(update);
which is what you do when you use the then.
from get_it.
I've rewritten initState method to this:
@override
void initState() async {
(await GetIt.I.getAsync<IAppModel>()).addListener(update);
super.initState();
}
Now I get this error message:
Connecting to VM Service at ws://127.0.0.1:40499/I25uskXCZ5E=/ws
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Builder:
_HomePageState.initState() returned a Future.
State.initState() must be a void method without an `async` keyword.
Rather than awaiting on asynchronous work directly inside of initState, call a separate method to do
this work without awaiting it.
So I rewrote it again:
@override
void initState() {
_adder();
super.initState();
}
void _adder() async => (await GetIt.I.getAsync<IAppModel>()).addListener(update);
Now I get this error message:
Connecting to VM Service at ws://127.0.0.1:33589/jvi4nx9cHWI=/ws
Rebuilt screen
Error: Bad state: You can only access registered factories/objects this way if they are created asynchronously
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 294:3 throw_
...
from get_it.
from get_it.
app_model.dart
import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
abstract interface class IAppModel extends ChangeNotifier {
void incrementcounter();
int get counter;
}
class AppModel extends IAppModel {
var _counter = 0;
AppModel() {
/// lets pretend we have to do some async initialization
Future
.delayed(const Duration(seconds: 3))
.then((_) => GetIt.I.signalReady(this))
;
}
@override
int get counter => _counter;
@override
void incrementcounter() {
_counter++;
notifyListeners();
}
}
di.dart
import 'package:get_it/get_it.dart';
import '../model/app_model.dart';
void configureDependencies() {
GetIt.I.registerSingleton<IAppModel>(AppModel(), signalsReady: true);
}
main.dart
void main() async {
configureDependencies();
runApp(const MyApp());
}
from get_it.
from get_it.
I'm sorry but I'm a little confused with your answer. In your example code you wrote:
// Alternative:
// getIt.getAsync<AppModel>().addListener(update);
This would indicate that this code
GetIt.I
.isReady<IAppModel>()
.then((_) => GetIt.I<IAppModel>().addListener(update))
;
can be replaced by this single line: getIt.getAsync<AppModel>().addListener(update);
however you suggest an additional change and it brings other changes and errors. After you say:
If you doesn't have any asynchronous inititialisation function, you don't need to use already or getAsync at all
So this alternative way you wrote in your example code doesn't apply to it, it's correct? If so, shouldn't there be another example where getAsync
is corectly used and not here that brings confusion? If I'm wrong, I ask you please to show me the correct way to use it in your code.
from get_it.
from get_it.
https://github.com/fluttercommunity/get_it/blob/master/example/lib/main.dart
line 50
from get_it.
from get_it.
I added a comment in the example for clarification
from get_it.
Related Issues (20)
- 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
- Concerns about Get_It HOT 4
- Need Help: Dependency Problem with GetIt in vm:entry-point Pragma 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.