如何使用getX在flutter get请求中传递参数

1aaf6o9v  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(188)

我实际上是在使用getX进行get请求,问题是我的一个端点需要我传递一个查询参数,而这将由应用程序用户完成。这就是事情的工作原理,当用户预订旅行并且预订成功时,将为该用户生成令牌,并且当用户想要查看旅行的详细信息时,他/她需要粘贴令牌,并且应用将使用令牌查询端点。以下是我根据别人的问题建议所做的尝试,但有一些错误

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());
    }
  }
}
3vpjnl9f

3vpjnl9f1#

// 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,)

相关问题