How does the dio package in flutter joins the base-url with url and apikey in this code

Viewed 456

How does the dio package in flutter joins the base-url with url and apikey in this code. Iam bit confused with the working of dio package can somebody please help me

If I have a URL of this kind

BaseURL = "https://newsapi.org/"
apkikey = "256c198308134b578e338737a895"
url= "v2/top-headlines?country=us"

How will dio package join the above three components to produce a url of this kind as shown below

https://newsapi.org/v2/top-headlines?country=us&apikey=256c198308134b578e338737a895

Any help would be appreciated

1 Answers

Something like that , ArticleResponse is model

BaseURL = "https://newsapi.org/v2/"
getTopHeadLinesUrl = "$BaseURL/top-headlines";
apkikey = "256c198308134b578e338737a895"

Future<ArticleResponse> getTopHeadlines() async {
    var params = {"apiKey": apikey, "country": "us"};
    try {
      Response response = await _dio.get(getTopHeadLinesUrl, queryParameters: params);
      return ArticleResponse.fromJson(response.data);
    } catch (e) {
      return ArticleResponse.withError(e.toString());
    }
  }

Example:
    "https://newsapi.org/v2/top-headlines?country=us&apiKey=509ea7c380274d5b9ae18c7a3fe9af7c"

Be aware that you have only 100 requests per day

Related