RefreshIndicator不会更新ListView数据

时间:2019-01-29 18:48:16

标签: dart flutter

我正在尝试使用RefreshIndicator更新ListView,但由于内容未更新,因此无法正常工作。我在屏幕上加载了一些初始数据l,当用户向下滚动时,我想加载一些额外的项目并将其添加到ListView的顶部。该请求正在运行,但是屏幕不是最新的。

我的代码:

Widget _answerTab(User user) {
    return FutureBuilder<void>(
      future: _getFirstQuestions(),
      builder: (context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.none:
          case ConnectionState.waiting:
            return Center(
              child: CircularProgressIndicator(),
            );
            break;
          default:
            return RefreshIndicator(
                key: _refreshIndicatorKey,
                onRefresh: _refresh,
                child: ListView.builder(
                    padding: EdgeInsets.all(10.0),
                    itemCount: questions.length,
                    itemBuilder: (context, index) {
                      return ExpandableCard();
                    }));
        }
      },
    );
  }

Future<void> _refresh() async {
    List<Question> nextQuestions =
        await QuestionHelper().getLastQuestions(_lastQuestionTimestamp);
    if (nextQuestions != null) {
      questions.insertAll(0, nextQuestions);
      _lastQuestionTimestamp = DateTime.fromMicrosecondsSinceEpoch(
          nextQuestions[nextQuestions.length - 1]
              .timestamp
              .millisecondsSinceEpoch);
    }
  }

  Future<void> _getFirstQuestions() async {
    questions = await QuestionHelper().getLastQuestions(_lastQuestionTimestamp);
  }

1 个答案:

答案 0 :(得分:0)

在_refresh方法中调用setState再次构建窗口小部件。

setState(() {
    questions.insertAll(0, nextQuestions);
    _lastQuestionTimestamp = DateTime.fromMicrosecondsSinceEpoch(
        nextQuestions[nextQuestions.length - 1]
        .timestamp
        .millisecondsSinceEpoch
    );
});