updates parameters to be of type Map<String, List<String>>

This commit is contained in:
Kevin Gray 2018-02-02 15:38:46 -05:00
parent a48211db08
commit 1639f4200e
4 changed files with 26 additions and 33 deletions

View File

@ -21,10 +21,10 @@ class Handler {
}
///
typedef Route<T> RouteCreator<T>(RouteSettings route, Map<String, dynamic> parameters);
typedef Route<T> RouteCreator<T>(RouteSettings route, Map<String, List<String>> parameters);
///
typedef Widget HandlerFunc(BuildContext context, Map<String, dynamic> parameters);
typedef Widget HandlerFunc(BuildContext context, Map<String, List<String>> parameters);
///
class AppRoute {

View File

@ -70,7 +70,7 @@ class Router {
///
Route<Null> _notFoundRoute(BuildContext context, String path) {
RouteCreator<Null> creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
RouteCreator<Null> creator = (RouteSettings routeSettings, Map<String, List<String>> parameters) {
return new MaterialPageRoute<Null>(settings: routeSettings, builder: (BuildContext context) {
return notFoundHandler.handlerFunc(context, parameters);
});
@ -93,13 +93,13 @@ class Router {
if (route == null && notFoundHandler == null) {
return new RouteMatch(matchType: RouteMatchType.noMatch, errorMessage: "No matching route was found");
}
Map<String, String> parameters = match?.parameters ?? <String, String>{};
Map<String, List<String>> parameters = match?.parameters ?? <String, List<String>>{};
if (handler.type == HandlerType.function) {
handler.handlerFunc(buildContext, parameters);
return new RouteMatch(matchType: RouteMatchType.nonVisual);
}
RouteCreator creator = (RouteSettings routeSettings, Map<String, dynamic> parameters) {
RouteCreator creator = (RouteSettings routeSettings, Map<String, List<String>> parameters) {
bool isNativeTransition = (transitionType == TransitionType.native || transitionType == TransitionType.nativeModal);
if (isNativeTransition) {
return new MaterialPageRoute<dynamic>(settings: routeSettings, fullscreenDialog: transitionType == TransitionType.nativeModal,

View File

@ -18,7 +18,7 @@ class AppRouteMatch {
// properties
AppRoute route;
Map<String, dynamic> parameters = <String, dynamic>{};
Map<String, List<String>> parameters = <String, List<String>>{};
}
class RouteTreeNodeMatch {
@ -26,7 +26,7 @@ class RouteTreeNodeMatch {
RouteTreeNodeMatch(this.node);
RouteTreeNodeMatch.fromMatch(RouteTreeNodeMatch match, this.node) {
parameters = <String, dynamic>{};
parameters = <String, List<String>>{};
if (match != null) {
parameters.addAll(match.parameters);
}
@ -34,7 +34,7 @@ class RouteTreeNodeMatch {
// properties
RouteTreeNode node;
Map<String, dynamic> parameters = <String, dynamic>{};
Map<String, List<String>> parameters = <String, List<String>>{};
}
class RouteTreeNode {
@ -121,7 +121,7 @@ class RouteTree {
List<RouteTreeNode> nextNodes = <RouteTreeNode>[];
for (RouteTreeNode node in nodesToCheck) {
String pathPart = checkComponent;
Map<String, dynamic> queryMap;
Map<String, List<String>> queryMap;
if (checkComponent.contains("?")) {
var splitParam = checkComponent.split("?");
pathPart = splitParam[0];
@ -133,7 +133,7 @@ class RouteTree {
RouteTreeNodeMatch match = new RouteTreeNodeMatch.fromMatch(parentMatch, node);
if (node.isParameter()) {
String paramKey = node.part.substring(1);
match.parameters[paramKey] = pathPart;
match.parameters[paramKey] = [pathPart];
}
if (queryMap != null) {
match.parameters.addAll(queryMap);
@ -211,25 +211,18 @@ class RouteTree {
return component.startsWith(":");
}
Map<String, dynamic> parseQueryString(String query) {
Map<String, List<String>> parseQueryString(String query) {
var search = new RegExp('([^&=]+)=?([^&]*)');
var params = new Map();
var params = new Map<String, List<String>>();
if (query.startsWith('?')) query = query.substring(1);
decode(String s) => Uri.decodeComponent(s.replaceAll('+', ' '));
for (Match match in search.allMatches(query)) {
String key = decode(match.group(1));
String value = decode(match.group(2));
if (params.containsKey(key)) {
dynamic paramValue = params[key];
if (paramValue is List<String>) {
paramValue.add(value);
} else if (paramValue is String) {
params[key] = [paramValue, value];
} else {
params[key] = value;
}
params[key].add(value);
} else {
params[key] = value;
params[key] = [value];
}
}
return params;

View File

@ -16,8 +16,8 @@ void main() {
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, String>{
"id" : "1234",
expect(match?.parameters, equals(<String, List<String>>{
"id" : ["1234"],
}));
});
@ -27,9 +27,9 @@ void main() {
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, String>{
"id" : "1234",
"name" : "luke",
expect(match?.parameters, equals(<String, List<String>>{
"id" : ["1234"],
"name" : ["luke"],
}));
});
@ -39,10 +39,10 @@ void main() {
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, String>{
"name" : "luke",
"phrase" : "hello world",
"number" : "7",
expect(match?.parameters, equals(<String, List<String>>{
"name" : ["luke"],
"phrase" : ["hello world"],
"number" : ["7"],
}));
});
@ -52,9 +52,9 @@ void main() {
Router router = new Router();
router.define(route, handler: null);
AppRouteMatch match = router.match(path);
expect(match?.parameters, equals(<String, dynamic>{
"name" : "luke",
"phrase" : "hello world",
expect(match?.parameters, equals(<String, List<String>>{
"name" : ["luke"],
"phrase" : ["hello world"],
"number" : ["7", "10", "13"],
}));
});