英文:
how to pass a parameter in flutter get request with getX
问题
我实际上在我的GET请求中使用了getX,问题是我的一个端点要求我传递一个查询参数,这将由应用用户完成。事情的运作方式如下,当用户预订行程并且预订成功时,将为该用户生成一个令牌,当用户想要查看行程的详细信息时,他/她需要粘贴令牌,应用程序将使用令牌查询端点。
以下是根据其他人的建议尝试的内容,但出现了一些错误
class ApiClient extends GetConnect implements GetxService {
late String token;
final String appBaseUrl;
late Map<String, String> _mainHeaders;
ApiClient({required this.appBaseUrl}) {
baseUrl = appBaseUrl;
timeout = Duration(minutes: 5);
token = AppUrlConstant.TOKEN;
_mainHeaders = {
'Content-type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer $token',
};
}
Future<Response> getDataWithParam(String url, String bToken) async {
try {
Map<String, String> parameter = {
"bookingToken": bToken
};
var queryUri = Uri(path: url, queryParameters: parameter);
Response response = await get(queryUri);
return response;
} catch (e) {
print("Error from the api client is " + e.toString());
return Response(statusCode: 1, statusText: e.toString());
}
}
}
英文:
I am actually using getX for my get request, the problem is that one of my endpoints require me to pass a query parameter and this will be done by the app user. This is how the thing works, when a user book a trip and the booking was successful, a token will be generated for that user and when the user want to view the details of the trip, he/she needs to paste the token and the app will query the endpoint using the token.
Here is what I tried based on suggestions from other people's problem but getting some errors
class ApiClient extends GetConnect implements GetxService{
late String token;
final String appBaseUrl;
late Map<String, String> _mainHeaders;
ApiClient({required this.appBaseUrl}){
baseUrl = appBaseUrl;
timeout = Duration(minutes: 5);
token = AppUrlConstant.TOKEN;
_mainHeaders = {
'Content-type':'application/json; charset=UTF-8',
'Authorization': 'Bearer $token',
};
}
Future<Response> getDataWithParam(String url,String bToken) async {
try{
Map<String,String> parameter = {
"bookingToken":bToken
};
var queryUri = Uri(path:url,queryParameters: parameter);
Response response = await get(queryUri);
return response;
}catch(e){
print("Error from the api client is "+e.toString());
return Response(statusCode: 1,statusText: e.toString());
}
}
}
答案1
得分: 0
"// lets consider this is your url and then add a parameter within the url
url = "https://stackoverflow.com/questions/76579395?bookingToken=${bToken}";
await get(Uri.parse(url))
// should be consider passing headers in get req too
await get(Uri.parse(url), headers: _mainHeaders,)"
英文:
// lets consider this is your url and then add a parameter within the url
url = "https://stackoverflow.com/questions/76579395?bookingToken=${bToken}";
await get(Uri.parse(url))
// should be consider passing headers in get req too
await get(Uri.parse(url), headers: _mainHeaders,)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论