【问题标题】:The onRefresh callback returned nullonRefresh 回调返回 null
【发布时间】:2021-04-25 14:54:29
【问题描述】:

我正在使用RefreshIndicator从互联网刷新数据,但是当我从顶部下拉时,刷新图标没有消失,也出现错误:

════════ 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.
════════════════════════════════════════════════════════════════════════════════

这是我的代码,其中声明了获取数据的方法:


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

这是我调用该方法的代码:

onRefresh: () {
  getApiData();
}

如果你能帮助我,我将非常感激。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    它会返回 null 因为你没有在这里放 await 所以它会立即返回结果(不等待 API 获取实际数据) 就这样吧

    onRefresh: () async {
      await getApiData();
    }
    

    【讨论】:

    • 错误消失但UI中显示的数据仍然没有改变。
    【解决方案2】:

    您的回调函数正在调用您的函数,但实际上并未返回它的结果。

    你可以这样做:

    onRefresh: () {
      return getApiData();
    }
    

    甚至更短:

    onRefresh: () => getApiData()
    

    或者因为你的签名匹配完全匹配,只是

    onRefresh: getApiData
    

    【讨论】:

      猜你喜欢
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多