Search code examples
flutterquery-stringflutter-getx

how to pass a parameter in flutter get request with getX


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

Solution

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