getViewByPath method

Future<View> getViewByPath(
  1. String path
)

Implementation

Future<View> getViewByPath(String path) async {
  if (path.isEmpty) {
    return homeView!;
  }
  final viewUrls = path.split('/');
  final firstUrl = viewUrls.removeAt(0);
  final firstViewUrlInfo = ViewUrlInfo.fromUrl(firstUrl);
  final firstView = registeredViewsMap[firstViewUrlInfo.id];
  if (firstView == null) {
    throw Exception('view "${firstViewUrlInfo.id}" is not registered');
  }
  firstView
    ..params = firstViewUrlInfo.params
    ..urlStateMap = firstViewUrlInfo.urlState;
  await firstView.init();
  var parentView = firstView;
  for (final viewUrl in viewUrls) {
    if (viewUrl.isNotEmpty) {
      final childViewUrlInfo = ViewUrlInfo.fromUrl(viewUrl);
      final childView = registeredViewsMap[childViewUrlInfo.id];
      if (childView == null) {
        throw Exception('view "${childViewUrlInfo.id}" is not registered');
      }
      childView
        ..parent = parentView
        ..params = childViewUrlInfo.params
        ..urlStateMap = childViewUrlInfo.urlState;
      await childView.init();
      parentView = childView;
    }
  }
  return parentView;
}