getByPathString<T> method

T getByPathString<T>(
  1. String path, {
  2. dynamic def,
})

if the path cannot be fully resolved, the method returns the provided default value. path A '/'-separated string representing the navigation path. def A default value of type T that is returned if the path cannot be resolved. Returns the value of type T at the specified path, or the default value if the path cannot be resolved. For example, the path is a String path for access to a node. for example "posts/news/1/title"

Implementation

T getByPathString<T>(String path, {dynamic def}) {
  if (path.endsWith('/')) {
    path = path.substring(0, path.length - 1);
  }

  if (path.startsWith('/')) {
    path = path.substring(1, path.length);
  }

  List<dynamic> arr = <dynamic>[];
  for (var element in Uri(path: path).pathSegments) {
    arr.add(int.tryParse(element) ?? element);
  }

  return getByPath<T>(arr, def: def);
}