Flutter Setstate inside Listview.builder's builditem function not reloading

前端 未结 1 1995
情歌与酒
情歌与酒 2021-01-26 15:39

I have a streambuilder that loads information from firestore. Then I use a listview.builder to display the information. The displayed information is being called by the function

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-26 15:55

    I solved the issue by turning firstBuildItem widget function into a stateful widget of it's own class. And then passing a key to the function. With this, setState worked without rebuilding the entire streambuilder.

    ListView.builder(
                      padding:    EdgeInsets.all(10.0),
                      itemCount:  snapshot.data.documents.length,
                      reverse:    true,
                      controller: listScrollController,
                      itemBuilder: (context, index) =>
    
                        index == snapLength ?
    
                        FirstChatBuildMessageItem(
                          key:          Key('counter-${index}'),
                          id:           id,
                          peerAvatar:   peerAvatar,
                          index:        index,
                          mainDocument: snapshot.data.documents[index],
                          listMessage: listMessage,
                        )
                            :
    
                        //index == 0 ?
    
                        ChatBuildMessageItem(
                          key:              Key('counter-${index}'),
                          id:               id,
                          peerAvatar:       peerAvatar,
                          index:            index,
                          mainDocument:     snapshot.data.documents[index],
                          previousDocument: snapshot.data.documents[index + 1],
                          listMessage:      listMessage,
                        ),
    
                    ),
    

    0 讨论(0)
提交回复
热议问题