find<S> method
Finds the registered type <S> (or tag)
In case of using Get.create to register a type <S> or tag,
it will create an instance each time you call find.
If the registered type <S> (or tag) is a Controller,
it will initialize it's lifecycle.
查找已注册的类型 <S> (或 tag)
如果使用 Get.create 注册类型 <S> 或 tag,
每次调用 find 时,它都会创建一个实例.
如果已注册的类型 <S> (或 tag) 是控制器,它将初始化其生命周期。
Implementation
S find<S>({String? tag}) {
// 根据类型和 tag 生成唯一 key
final key = _getKey(S, tag);
// 如果已经注册过
if (isRegistered<S>(tag: tag)) {
// 取出对应的实例
final dep = _singl[key];
// 如果实例为空, 抛出异常
if (dep == null) {
if (tag == null) {
throw 'Class "$S" is not registered';
} else {
throw 'Class "$S" with tag "$tag" is not registered';
}
}
/// although dirty solution, the lifecycle starts inside
/// `initDependencies`, so we have to return the instance from there
/// to make it compatible with `Get.create()`.
/// 虽然是 dirty 的解决方案,但生命周期是在 `initDependencies` 内部开始的,
/// 因此我们必须从那里返回实例,以便使其与 `Get.create()` 兼容.
final i = _initDependencies<S>(name: tag);
return i ?? dep.getDependency() as S;
} else {
// 如果没有注册过, 抛出异常
// ignore: lines_longer_than_80_chars
throw '"$S" not found. You need to call "Get.put($S())" or "Get.lazyPut(()=>$S())"';
}
}