matchRoute method
Matches a given name
to the appropriate route in the tree and returns a RouteDecoder.
Implementation
RouteDecoder matchRoute(String name, {PageSettings? arguments}) {
final uri = Uri.parse(name);
final split = uri.path.split('/').where((element) => element.isNotEmpty);
var curPath = '/';
final cumulativePaths = <String>[];
for (var item in split) {
curPath = curPath.endsWith('/') ? curPath + item : '$curPath/$item';
cumulativePaths.add(curPath);
}
if (cumulativePaths.isEmpty) {
cumulativePaths.add('/');
}
final treeBranch = cumulativePaths
.map((e) => MapEntry(e, _findRoute(e)))
.where((element) => element.value != null)
.map((e) => MapEntry(e.key, e.value!.copyWith(key: ValueKey(e.key))))
.toList();
final params = Map<String, String>.from(uri.queryParameters);
if (treeBranch.isNotEmpty) {
final lastRoute = treeBranch.last;
final parsedParams = _parseParams(name, lastRoute.value.path);
if (parsedParams.isNotEmpty) {
params.addAll(parsedParams);
}
final mappedTreeBranch = treeBranch
.map(
(e) => e.value.copyWith(
parameters: {
if (e.value.parameters != null) ...e.value.parameters!,
...params,
},
name: e.key,
),
)
.toList();
arguments?.params.clear();
arguments?.params.addAll(params);
return RouteDecoder(
mappedTreeBranch,
arguments,
);
}
arguments?.params.clear();
arguments?.params.addAll(params);
return RouteDecoder(
treeBranch.map((e) => e.value).toList(),
arguments,
);
}