【发布时间】:2020-10-15 16:02:42
【问题描述】:
我的颤振应用程序中有一个方法可以通过将参数传递到 url 来查询 get 请求
Future<Map<String, dynamic>> getData(
{String path, String token, Map<String, String> params}) async {
try {
Uri uri = Uri.parse(path);
Uri newUri = uri.replace(queryParameters: params); // http://some/path/?param=1...
final http.Response response =
await http.get(newUri, headers: APIHeader.authorization(token));
final jsonResponse = json.decode(response.body);
if (response.statusCode != 200) {
throw ServerException(jsonResponse["error"]);
}
return jsonResponse['result'];
} catch (error) {
throw error;
}
}
其中Uri方法生成的url是http://some/path/?param=1...
它工作得很好,但是如果我只想用它来查询 id 那么查询参数格式只是 id
http://some/path/1
如果我使用上面的方法并以{'id':'1'} 格式发送参数,那么我会得到 url
http://some/path/?id=1
有没有办法在我的后端强制这些参数采用../?id=1 格式,或者有没有办法让Uri 方法识别这些差异?
我的 id 的后端路由器是
router.get('/some/path/:id', controller.get);
【问题讨论】: