Bug fix for async singleton factory method and fixing example code #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There were several errors in the example code that were causing it to throw errors.
initState
method was callingget
directly instead ofgetAsync
, and there's no way the future started in the singleton initialization was going to be done by then. This caused the "You tried to access an instance of AppModel that was not ready yet" error to be thrown.FloatingActionButton
was declared outside of theFutureBuilder
but still attempted to callget
on the resource. This was resolved by moving theFutureBuilder
to the top of the returned widget tree.Furthermore, there was an error in the
_register
method inGetItImpl
when callingregisterSingletonAsync
with a provider that returned an instance ofT
rather than aFuture<T>
.The method was assigning
instance
toserviceFactory.instance
, but when_register
is called viaregisterSingletonAsync
.instance
is defined to be null. This caused an issue later on when callingget
, as a value of typeNull
cannot be coerced toAppModel
(i.e.null is AppModel
will always return false), leading to an error to be thrown with the confusing message: "Object with name has a different type (AppModel) than the one that is inferred (AppModel) where you call it".This was fixed by changing the line:
serviceFactory.instance = instance;
to:serviceFactory.instance = asyncResult;