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