Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling an API get request, can't access the function

I am trying to fetch API data using http client. I tried to follow this example https://docs.flutter.dev/cookbook/networking/fetch-data

But I am getting the following error whats on the image. Also, do I need to add ? after the Character? Because otherwise it won’t work, but all the examples I look at don’t have that?

class HttpManager {
   static String baseUrl = "https://rickandmortyapi.com/api/character";
   var client = http.Client();

   Future<Character?> getCharacters() async {
      var response = await client.get(Uri.parse(baseUrl));

      if (response.statusCode == 200) {
         return Character.fromJson(jsonDecode(response.body));
      }
   }

}

Then in my class I try to use it
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Actually the function you have defined in HttpManager is not a static function that is why you can not call that function without creating a object of that class so you need to call the function like below

HttpManager().getCharacters();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading