首页 > 解决方案 > 为什么当我向下滚动时我的颤动滚动监听器会多次调用

问题描述

每次滚动以获取新数据时,我都有重复的数据,如果我只在文档末尾执行一次滚动但在最后滚动多次时会出现重复,则它工作正常。我假设如果数据没有按时加载,我的用户可能会滚动多次

Future<List<DocumentSnapshot>> getMoreSearchDocuments(lastDocument) async{
    querySnapshot = await Firestore.instance.collection('val').orderBy('dateCreated', descending:true)
       .startAfterDocument(lastDocument).limit(documentLimit).getDocuments();


    return querySnapshot.documents;
  } 

void _scrollListener() async{
      if(_scrollController.offset >=
            _scrollController.position.maxScrollExtent &&
        !_scrollController.position.outOfRange) {
          DocumentSnapshot lastDocument = widget.searchDocuments[widget.searchDocuments.length - 1];
      setState((){
        isLoading = true ;
        print('You are at the end of the document');
      });
      await api.getMoreSearchDocuments(lastDocument)
            .then((documentSnapshots){
              setState(() {
                widget.searchDocuments.addAll(documentSnapshots);  
                isLoading = false;
              });
            })
            .catchError((e) {
          print(e);
      });
    }
  }

@override
  void initState() {
    _scrollController.addListener(_scrollListener);
    super.initState();
  }


SafeArea(
           child: CustomScrollView(
              controller: _scrollController,
              slivers: <Widget>[
                SliverAppBar(
                  title: Text('Rx finder'),
                  actions: <Widget>[
                    IconButton(
                      icon: Icon(Icons.search),
                      onPressed: () =>
                          showSearch(context: context, delegate: SearchListings()),
                          ....................

My Console Date which indicate the data is being fetched twice

I/flutter (25676): You are at the end of the document
I/flutter (25676): Second list
D/ViewRootImpl(25676): ViewPostImeInputStage processPointer 0
I/flutter (25676): You are at the end of the document
I/flutter (25676): Second list

标签: flutterscrollgoogle-cloud-firestore

解决方案


推荐阅读