ListView.builder itemCount未更新

ListView.builder itemCount未更新,listview,flutter,builder,Listview,Flutter,Builder,我有一个小部件,它返回ListView.builder,当用户到达ListView的底部时,ListView的项作为小部件的参数传递,并加载新项。但是当项目被加载时,itemCount不会更新,如果打印value参数,我会得到正确数量的项目,但是我会得到一个索引错误,前面的值作为范围。。。 这是我的日志: I/flutter ( 4654): 4 I/flutter ( 4654): 8 ════════ Exception caught by widgets library ════════

我有一个小部件,它返回ListView.builder,当用户到达ListView的底部时,ListView的项作为小部件的参数传递,并加载新项。但是当项目被加载时,itemCount不会更新,如果打印value参数,我会得到正确数量的项目,但是我会得到一个索引错误,前面的值作为范围。。。 这是我的日志:

I/flutter ( 4654): 4
I/flutter ( 4654): 8

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following RangeError was thrown building:
RangeError (index): Invalid value: Not in range 0..3, inclusive: 4

When the exception was thrown, this was the stack: 
#0      List.[] (dart:core-patch/growable_array.dart:146:60)
#1      _UsersListViewState.showFollowButton (package:the_spot/services/library/usersListView.dart:167:30)
#2      _UsersListViewState.showResultWidget (package:the_spot/services/library/usersListView.dart:147:43)
#3      _UsersListViewState.build.<anonymous closure> (package:the_spot/services/library/usersListView.dart:75:20)
#4      SliverChildBuilderDelegate.build (package:flutter/src/widgets/sliver.dart:446:22)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
I/颤振(4654):4
I/颤振(4654):8
════════ widgets库捕获到异常═══════════════════════════════════════════════════════
在生成时引发了以下RangeError:
RangeError(索引):无效值:不在范围0..3中,包括:4
引发异常时,这是堆栈:
#0列表。[](dart:core patch/Growtable_阵列。dart:146:60)
#1 _UsersListViewState.showFollowButton(包:the_spot/services/library/usersListView.dart:167:30)
#2 _UsersListViewState.showResultWidget(包:the _spot/services/library/usersListView.dart:147:43)
#3_UsersListViewState.build。(软件包:the_spot/services/library/usersListView.dart:75:20)
#4 SliverChildBuilderDelegate.build(包:flatter/src/widgets/sliver.dart:446:22)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
查看第一次加载项目时,它会打印项目计数(4),并且没有发生错误(在我的屏幕上,我也有项目列表),但是当用户向下滚动以更新项目列表时,它会打印新的项目计数(8),但范围仍然是4

这里是小部件代码(我删除了不必要的内容):

导入“包装:颤振/材料.省道”;
导入“package:the_spot/pages/home_page/profile.dart”;
导入“package:the_spot/services/library/userProfile.dart”;
导入“包:_spot/services/library/configuration.dart”;
导入“../theme.dart”;
导入“../database.dart”;
导入“library.dart”;
类UsersListView扩展StatefulWidget{
最终配置;
最终列表查询;
BottomListReachedCallback的最终作废回调;
constUsersListView({Key-Key,this.configuration,this.query,this.onbottomListReacheCallback}):super(Key:Key);
@凌驾
_UsersListViewState createState()=>\u UsersListViewState();
}
类_UsersListViewState扩展状态{
列表waitForFollowing=[];
列出FriendRequestalReadyOne=[];
列表waitForSendingFriendRequest=[];
bool isLoadingData=false;
@凌驾
void initState(){
super.initState();
waitForFollowing.clear();
widget.query.forEach((元素){
waitForFollowing.add(false);
waitForSendingFriendRequest.add(false);
if(element.pendingFriendsId
.indexOf(widget.configuration.userData.userId)=
-1) {
friendRequestAlreadyDone.add(true);
}否则{
friendRequestAlreadyDone.add(false);
}
});
}
@凌驾
void didUpdateWidget(UsersListView oldWidget){
super.didUpdateWidget(oldWidget);
isLoadingData=false;
}
@凌驾
小部件构建(构建上下文){
打印(widget.query.length);
扩大回报(
孩子:NotificationListener(
onNotification:(滚动通知滚动信息){
if(scrollInfo.metrics.pixels==scrollInfo.metrics.maxScrollExtent&&isLoadingData==false){
isLoadingData=true;
onBottomListReachedCallback();
}
返回true;
},
子项:ListView.builder(
填充:EdgeInsets.fromLTRB(
widget.configuration.screenWidth/20,
widget.configuration.screenWidth/40,
widget.configuration.screenWidth/20,
widget.configuration.screenWidth/40),
itemCount:widget.query.length,
itemBuilder:(BuildContext上下文,int itemIndex){
返回showResultWidget(itemIndex);
},
收缩膜:假,
),
),
);
}
编辑: 如所问,小部件是如何调用的:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:the_spot/services/database.dart';
import 'package:the_spot/services/library/configuration.dart';
import 'package:the_spot/services/library/userProfile.dart';
import 'package:the_spot/services/library/usersListView.dart';
import 'package:the_spot/theme.dart';


class FollowersFollowingFriendsPage extends StatefulWidget {
  final Configuration configuration;
  final UserProfile userProfile;
  final String type;

  const FollowersFollowingFriendsPage(
      {Key key, this.configuration, this.userProfile, this.type})
      : super(key: key);

  @override
  _FollowersFollowingFriendsPageState createState() =>
      _FollowersFollowingFriendsPageState();
}

class _FollowersFollowingFriendsPageState
    extends State<FollowersFollowingFriendsPage> {
  bool isWaiting = true;
  List<UserProfile> queryResult = [];
  Timestamp index = Timestamp.now();

  String noResultMessage;
  String appBarTitle;

  @override
  void initState() {
    super.initState();
    init();



  }

  void init() async {
    switch (widget.type) {
      case "Followers":
        {
          noResultMessage =
          "This user haven't been followed by anyone for the moment.";
          appBarTitle = "Users following " + widget.userProfile.pseudo;

          Map<String, Object> res = await Database().getFollowersOf(context, widget.configuration.userData.userId, widget.userProfile.userId, index, 10);

          queryResult.addAll(res['users']);
          index = res['lastTimestamp'];
          setState(() {
            isWaiting = false;
          });

        }
        break;

      case "Following":
        {
          noResultMessage = "This user doesn't follow anyone for the moment.";
          appBarTitle = "Users followed by " + widget.userProfile.pseudo;

          Map<String, Object> res = await Database().getFollowingOf(context, widget.configuration.userData.userId, widget.userProfile.userId, index, 4);

          queryResult.addAll(res['users']);

          index = res['lastTimestamp'];
          setState(() {
            isWaiting = false;
          });
        }
        break;
      case "Friends":
        {
          noResultMessage = "This user hasn't added friends yet.";
          appBarTitle = "Friends of " + widget.userProfile.pseudo;

          queryResult = await Database().getUsersByIds(context, widget.userProfile.friends, verifyIfFriendsOrFollowed: true, mainUserId: widget.userProfile.userId);
          setState(() {
            isWaiting = false;
          });
        }
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: PrimaryColorDark,
      appBar: AppBar(title: Text(appBarTitle),),
      body: Column(
        children: <Widget>[
          showQueryResultsWidget(),
        ],
      ),
    );
  }

  Widget showQueryResultsWidget() {
    if (isWaiting)
      return Padding(
          padding: EdgeInsets.only(top: widget.configuration.screenWidth / 20),
          child: Center(
            child: CircularProgressIndicator(),
          ));
    else if (queryResult.length == 0 || queryResult == null)
      return Padding(
        padding: EdgeInsets.only(top: widget.configuration.screenWidth / 20),
        child: Center(child: Text(noResultMessage)),
      );
    else
      return UsersListView(
        configuration: widget.configuration,
        query: queryResult,
        onBottomListReachedCallback: init,
      );
  }
}
import'包:cloud_firestore/cloud_firestore.dart';
进口“包装:颤振/材料.省道”;
导入“包:_spot/services/database.dart”;
导入“包:_spot/services/library/configuration.dart”;
导入“package:the_spot/services/library/userProfile.dart”;
导入“包:_spot/services/library/usersListView.dart”;
导入“package:the_spot/theme.dart”;
类followersFollowFriendsPage扩展StatefulWidget{
最终配置;
最终用户配置文件用户配置文件;
最终字符串类型;
常数跟随者(
{Key Key,this.configuration,this.userProfile,this.type})
:super(key:key);
@凌驾
_FollowersFollowFriendsPagestate createState()=>
_followers followers friendspagestate();
}
第_类跟随者
扩展状态{
bool isWaiting=true;
列表查询结果=[];
Timestamp index=Timestamp.now();
字符串noResultMessage;
字符串appBarTitle;
@凌驾
void initState(){
super.initState();
init();
}
void init()异步{
开关(widget.type){
“追随者”一案:
{
noResultMessage=
“目前还没有任何人跟踪此用户。”;
appBarTitle=“用户跟随”+widget.userProfile.pseudo;
Map res=await Database().getFollowersOf(上下文,widget.configuration.userData.userId,widget.userProfile.userId,索引,10);
addAll(res['users']);
索引=res['lastTimestamp'];
设置状态(){
isWaiting=false;
});
}
打破
“以下”情况:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:the_spot/services/database.dart';
import 'package:the_spot/services/library/configuration.dart';
import 'package:the_spot/services/library/userProfile.dart';
import 'package:the_spot/services/library/usersListView.dart';
import 'package:the_spot/theme.dart';


class FollowersFollowingFriendsPage extends StatefulWidget {
  final Configuration configuration;
  final UserProfile userProfile;
  final String type;

  const FollowersFollowingFriendsPage(
      {Key key, this.configuration, this.userProfile, this.type})
      : super(key: key);

  @override
  _FollowersFollowingFriendsPageState createState() =>
      _FollowersFollowingFriendsPageState();
}

class _FollowersFollowingFriendsPageState
    extends State<FollowersFollowingFriendsPage> {
  bool isWaiting = true;
  List<UserProfile> queryResult = [];
  Timestamp index = Timestamp.now();

  String noResultMessage;
  String appBarTitle;

  @override
  void initState() {
    super.initState();
    init();



  }

  void init() async {
    switch (widget.type) {
      case "Followers":
        {
          noResultMessage =
          "This user haven't been followed by anyone for the moment.";
          appBarTitle = "Users following " + widget.userProfile.pseudo;

          Map<String, Object> res = await Database().getFollowersOf(context, widget.configuration.userData.userId, widget.userProfile.userId, index, 10);

          queryResult.addAll(res['users']);
          index = res['lastTimestamp'];
          setState(() {
            isWaiting = false;
          });

        }
        break;

      case "Following":
        {
          noResultMessage = "This user doesn't follow anyone for the moment.";
          appBarTitle = "Users followed by " + widget.userProfile.pseudo;

          Map<String, Object> res = await Database().getFollowingOf(context, widget.configuration.userData.userId, widget.userProfile.userId, index, 4);

          queryResult.addAll(res['users']);

          index = res['lastTimestamp'];
          setState(() {
            isWaiting = false;
          });
        }
        break;
      case "Friends":
        {
          noResultMessage = "This user hasn't added friends yet.";
          appBarTitle = "Friends of " + widget.userProfile.pseudo;

          queryResult = await Database().getUsersByIds(context, widget.userProfile.friends, verifyIfFriendsOrFollowed: true, mainUserId: widget.userProfile.userId);
          setState(() {
            isWaiting = false;
          });
        }
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: PrimaryColorDark,
      appBar: AppBar(title: Text(appBarTitle),),
      body: Column(
        children: <Widget>[
          showQueryResultsWidget(),
        ],
      ),
    );
  }

  Widget showQueryResultsWidget() {
    if (isWaiting)
      return Padding(
          padding: EdgeInsets.only(top: widget.configuration.screenWidth / 20),
          child: Center(
            child: CircularProgressIndicator(),
          ));
    else if (queryResult.length == 0 || queryResult == null)
      return Padding(
        padding: EdgeInsets.only(top: widget.configuration.screenWidth / 20),
        child: Center(child: Text(noResultMessage)),
      );
    else
      return UsersListView(
        configuration: widget.configuration,
        query: queryResult,
        onBottomListReachedCallback: init,
      );
  }
}
 void init() async {
switch (widget.type) {
  case "Followers":
  ....
  setState((){
   queryResult.addAll(res['users']);
   ...
 });