waitState<T extends State<StatefulWidget> > function
FutureOr<void>
waitState<T extends State<StatefulWidget> >(
- FutureOr<
void> run(- T
- StateGetter<
T> stateGetter, - VoidCallback onFailed
在同一个代码块中读取值
确保初始化时,state.mounted == true
通过throw
退出循环
Implementation
FutureOr<void> waitState<T extends State>(FutureOr<void> Function(T) run,
StateGetter<T> stateGetter, VoidCallback onFailed) async {
Timer? timer;
assert(() {
timer = Timer(const Duration(seconds: 10), () {
Log.e('获取`overlay`过长`overlayGetter`长时间无返回值', position: 2);
});
return true;
}());
while (true) {
try {
final overlaystate = stateGetter();
if (overlaystate != null && overlaystate.mounted) {
assert(() {
timer?.cancel();
return true;
}());
return run(overlaystate);
}
} on OverlayGetterError catch (e) {
assert(Log.i(e));
onFailed();
break;
} catch (e) {
onFailed();
Log.e('unKnownError: $e');
break;
}
await waitForFrame();
}
assert(() {
timer?.cancel();
return true;
}());
}