Search code examples
androidiosflutterflutter-getx

Invalid argument(s): No host specified in URI flutter getconnect


I'm trying to use the Get Connect package to make an API call from my flutter application that totally depends on GetX .

Here is the code am using :

class OnBoardingProvider extends GetConnect {
  @override
  void onInit() {
    httpClient.baseUrl = 'https://www.my_domain_name.com/';
    httpClient.addRequestModifier((request) {
      request.headers['lang'] = Get.locale.languageCode;
      return request;
    });
    if (UserModelProvider().checkForLogin()) {
      httpClient.addAuthenticator((request) {
        String token = UserModelProvider().getToken();
        request.headers['Authorization'] = "Token $token";
        return request;
      });
    }
    super.onInit();
  }

  Future<List<dynamic>> getOnBoarding() async {
    final response = await get('static-pages/api/on_boarding/');
    return response.body;
  }

but it keeps showing me the error

Unhandled Exception: Invalid argument(s): No host specified in URI static-pages/api/on_boarding/

that means the line httpClient.baseUrl = ... didn't affect the code .. or maybe am calling it the wrong way !!

UPDATE

am calling the instance of OnBoardingProvider this way when I call the function :

    OnBoardingProvider().getOnBoarding().then((value) {
      print(value.body);
    });

Solution

  • The method onInit is not getting called, that's why baseUrl didn't get initialised.

    The method 'onInit' is called when you inject the dependency using Get.put(Controller()); into the widget tree.

    So,

    OnBoardingProvider onBoardingProvider = Get.put(onBoardingProvider());
    
     onBoardingProvider.getOnBoarding().then((value) {
          print(value.body);
        });
    
    

    More about depenceny injection in GetX and Get Connect