如何在Flutter的GetX中通过GET请求传递参数。

huangapple go评论60阅读模式
英文:

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&lt;String, String&gt; _mainHeaders;

  ApiClient({required this.appBaseUrl}){
    baseUrl = appBaseUrl;
    timeout = Duration(minutes: 5);
    token = AppUrlConstant.TOKEN;
    _mainHeaders = {
      &#39;Content-type&#39;:&#39;application/json; charset=UTF-8&#39;,
      &#39;Authorization&#39;: &#39;Bearer $token&#39;,
    };
  }

Future&lt;Response&gt; getDataWithParam(String url,String bToken) async {
    try{
      Map&lt;String,String&gt; parameter = {
        &quot;bookingToken&quot;:bToken
      };
      var queryUri = Uri(path:url,queryParameters: parameter);
      Response response = await get(queryUri);

      return response;
    }catch(e){
      print(&quot;Error from the api client is &quot;+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 = &quot;https://stackoverflow.com/questions/76579395?bookingToken=${bToken}&quot;;

await get(Uri.parse(url))

// should be consider passing headers in get req too
await get(Uri.parse(url), headers: _mainHeaders,)

huangapple
  • 本文由 发表于 2023年6月29日 16:37:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定