Skip to content

Bug fix for async singleton factory method and fixing example code #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

andrewackerman
Copy link

There were several errors in the example code that were causing it to throw errors.

  • The initState method was calling get directly instead of getAsync, 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.
  • Similarly, the FloatingActionButton was declared outside of the FutureBuilder but still attempted to call get on the resource. This was resolved by moving the FutureBuilder to the top of the returned widget tree.

Furthermore, there was an error in the _register method in GetItImpl when calling registerSingletonAsync with a provider that returned an instance of T rather than a Future<T>.

  • The method was assigning instance to serviceFactory.instance, but when _register is called via registerSingletonAsync. instance is defined to be null. This caused an issue later on when calling get, as a value of type Null cannot be coerced to AppModel (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;

… instance as null

Example project runs without errors now
@escamoteur
Copy link
Collaborator

Sorry, I somehow missed your PR (thanks for doing it) I made a general overhaul in the meantime and checked the parts that you reported, Please give #46 a try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants