Description
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?