StreamBuilder + ListView.builder + Firebase Realtime Database是否限制数据流量?

时间:2019-04-30 19:26:37

标签: firebase firebase-realtime-database flutter flutter-dependencies

我有一个聊天应用程序,当我在群聊中有例如10.000条消息(+图片)时,我有点担心。以下代码是否正确(或者每次打开时都加载了整个聊天(=所有消息))?一个很好的解决方案是,如果StreamBuilder仅加载最新消息,而如果我滚动到较旧的消息,则仅延迟加载。

 StreamBuilder(
                  stream: _queryDatabase.onValue,
                  builder: (BuildContext context, snapshot) {
                    if (snapshot.hasData) {
                      if (snapshot.data.snapshot.value != null) {
                        //final int documentsLength = snapshot.data.documents.length;
                        Map<dynamic, dynamic> map =
                            snapshot.data.snapshot.value;

                        List<dynamic> list = map.values.toList()
                          ..sort((a, b) =>
                              b['timestamp'].compareTo(a['timestamp']));
                        final int listLength = map.values.toList().length;
                        // map.forEach((dynamic, v) => print(v["author"]));
                        //print(messages);

                        return ListView.builder(
                            padding: const EdgeInsets.all(8.0),
                            physics: const AlwaysScrollableScrollPhysics(),
                            controller: _scrollController,
                            itemCount: listLength,
                            reverse: true,
                            itemBuilder: (context, int index) {
                              _responseToText() {
                                setRef(
                                    textRef: list[index]['text'],
                                    authorRef: list[index]['author']);
                              }

                              _responseToImage() {
                                setRef(
                                    imageUrl: list[index]['imageUrl'],
                                    authorRef: list[index]['author']);
                              }

                              _responseToImageResponse() {
                                setRef(
                                    textRef: list[index]['text'],
                                    imageUrl: list[index]['imageUrl'],
                                    authorRef: list[index]['author']);
                              }

                              return ChatMessageSingleChat(
                                snapshot: list,
                                index: index,
                                userId: globals.userId,
                                singleChat: true,
                                groupId: widget.groupId,
                                answerText: _responseToText,
                                answerImage: _responseToImage,
                                answerImageAnswer: _responseToImageResponse,
                                newFocusNode: _newFocusNode,
                              );
                            }); ...

0 个答案:

没有答案