Skip to content

.get() does not return instance when registered with .registerSingletonAsync  #121

Closed
@DirtyNative

Description

@DirtyNative

I made a new project with a model class:

class Model {
  int i;

  Future<Model> init() async {
    await Future.delayed(Duration(seconds: 1));
    i = 10;
    return this;
  }
}

and a service locator file

import 'package:get_it/get_it.dart';

import 'model.dart';

void setup() {
  var getIt = GetIt.instance;

  getIt.registerSingletonAsync<Model>(() async {
    var model = Model();
    model.init();

    return model;
  }, signalsReady: true);
}

The setup method is called right before runApp(MyApp());

When setting singnalsReady to true I get an erorr:

════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown building MyHomePage(dirty, state: _MyHomePageState#02fe8): You tried to access an instance of Model that was not ready yet 'package:get_it/get_it_impl.dart': Failed assertion: line 322 pos 14: 'instanceFactory.isReady'

And when I set singnalsReady to false, GetIt returns an instance though the init() function did not finish, so the int inside is still null. As I have read in the docmentation, GetIt should run the async function first, and then return the instance from the get() method. Is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions