dartdart-shelfdart-server

dart, shelf_route: What is route name in the `GET` method that work with the request that using multi parameter?


My flutter app sends the request to this API with the GET method and uses the queryParameters like this:

Map<String, dynamic> parameters = {'id': null};
parameters.updateAll((key, value) => Uri.encodeComponent(jsonEncode(value)));
String url= 'http://localhost:8080/test;
final Uri uri = Uri.parse(url).replace(queryParameters: parameters);
final http.Response response = await http.get(uri, headers: headers);

The URL that was sent is http://localhost:8080/test?id=null

My route on the server side is:

final _route = shelf_router.Router()
  ..get("/test<id>", (Request request, String id) {
    return Response.ok('ok');
  })
  ..all('/<ignored|.*>', (Request request) {
    return Response.notFound('notFound');
  });

But my request from the flutter app always goes to the all('/<ignored|.*>', (Request request){ ... } route. I have tried to change the route name in the get("/test<id>", (Request request, String id){ ... } from "/test<id>" to "/test<id>", "/test?<id>", "/test?id<id>", or "/test?id=<id>" but there isn't working.

What is the correct route name?

And I want to use the route with n parameters(get these parameters by using request.params[ ... ]). What is the route name to do that?


Solution

  • OMG, I missed this. Just use the "/test".

    The parameters are request.url.queryParameters

    example:

    ..get("/test", (Request request) {
      Map<String, dynamic> parameters = {...request.url.queryParameters};
      parameters.updateAll((key, value) => jsonDecode(Uri.decodeComponent(value)));
      print(parameters); //{id: null}
      return Response.ok('ok');
    })