0

I am using RefreshIndicator to refresh data from the internet, but when I pulled down from the top, the refresh icon didn't disappear, also an error occured:

════════ Exception caught by material library ══════════════════════════════════
The following assertion was thrown when calling onRefresh:
The onRefresh callback returned null.

The RefreshIndicator onRefresh callback must return a Future.
════════════════════════════════════════════════════════════════════════════════

Here's my code where the method to get data is declared:


Future<void> getApiData() async {
  return fetchWorldData();
}

Future<Map<String, dynamic>> fetchWorldData() async {
  Response response = await get(Uri.parse(
      'https://disease.sh/v3/covid-19/countries/${CurrentCountry.currentcountry}'));
  return json.decode(response.body);
}

And here's my code where the method is called:

onRefresh: () {
  getApiData();
}

If you can help me, I will be very grateful.

2 Answers 2

1

It will return null as you didn't put await here so it immediately returns the result (not waiting for API to get the actual data) So do it like this

onRefresh: () async {
  await getApiData();
}
1
  • The error disappeared but the data shown in the UI still doesn't change. Commented Apr 26, 2021 at 13:13
0

Your callback function is calling your function, but not actually returning it's result.

You could do:

onRefresh: () {
  return getApiData();
}

or even shorter:

onRefresh: () => getApiData()

or since your signatures match exactly, just

onRefresh: getApiData

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.