of static method
静态方法获取 GetRootState
Implementation
static GetRootState of(BuildContext context) {
// Handles the case where the input context is a navigator element.
GetRootState? root;
if (context is StatefulElement && context.state is GetRootState) {
root = context.state as GetRootState;
}
// 从所有祖先中查到最接近根的 [GetRootState] 类型祖先
// findRootAncestorStateOfType 就是遍历所有祖先并返回找到的最后一个
root = context.findRootAncestorStateOfType<GetRootState>() ?? root;
assert(() {
if (root == null) {
throw FlutterError(
'GetRoot operation requested with a context that does not include a GetRoot.\n'
'The context used must be that of a '
'widget that is a descendant of a GetRoot widget.',
);
}
return true;
}());
return root!;
}