0

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);
    });
2
  • It might be because of onInit() not getting called hence baseUrl not getting initialised. Can you add about how your are creating instance of class OnBoardingProvider ? Commented Jan 27, 2022 at 11:11
  • @SahilHariyani added it to the question, thank you Commented Jan 27, 2022 at 11:45

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.