gpt4 book ai didi

listview - Flutter/Firestore - 在 streambuilder 中的动态 ListView 上实现 "pagination"

转载 作者:IT王子 更新时间:2023-10-29 07:03:02 24 4
gpt4 key购买 nike

我想通了逻辑并且它有效,但我遗漏了一些东西,因为唯一有效的方法是当我用下一个“页面”或文档替换数组时,但是当我执行 .addAll() 到它,它变得困惑。相同的第一个文件被重新添加,一些新的文件也被包括在内,但不是所有应该包括的。

代码:

ScrollController _scrollController = ScrollController();
List<Message> _messages = [];
List<dynamic> _startAfter = [DateTime.now()];

...

@override
void initState() {
super.initState();
this._scrollController.addListener(() {
if (this._scrollController.position.minScrollExtent + this._scrollController.position.pixels <= -15.0) {
setState(() {
this._startAfter = [this._messages.first.createdAt.toDate()];
});
}
});
}

...

Column(
children: <Widget>[
Expanded(
child: StreamBuilder<List<Message>>(
stream: APIs().chats.messagesStream(chatID: widget.chat.chatID, orderBy: 'createdAt', descending: true, startAfter: this._startAfter, limit: 10),
builder: (context, snapshot) {
print(this._startAfter);
switch (snapshot.connectionState) {
case ConnectionState.waiting:
{
return PAIndicator();
}
default:
{
if (snapshot.hasData) {
this._messages = snapshot.data; // Having the issue here.

// this._messages.addAll(snapshot.data); Causes problems when rebuilding

return MessagesList(
scrollController: this._scrollController,
messages: this._messages,
aUser: widget.aUser,
);
} else {
return ListView();
}
}
}
}),
),
// ...
]
)

我注意到该错误仅在重建时发生。

当检索到数据时,它会替换数组中的内容,当 .addAll() 为使用而不用担心 ui 被重建,就像键盘显示和关闭一样?

此外,如果没有更多文档,我如何确保它不会被重建或调用 firebase? (解决了,现在就剩下上面的主要问题了)

最佳答案

为任何需要它的人找出解决方案。唯一的问题是这些项目只是被添加到列表中,所以它看起来不太适合 UI/UX。如果你们都有解决方案,请随意添加该解决方案。我尝试了 AnimatedList,但由于状态问题而没有奏效,而且我还不知道有什么解决方法。

注意:由于某些原因,它没有收听任何一位用户的新消息,我必须重新进入聊天才能看到新消息,请帮忙。

GlobalKey<AnimatedListState> _listKey = GlobalKey();
ScrollController _scrollController = ScrollController();
List<Message> _messages = [];
List<dynamic> _startAfter = [DateTime.now()];
int _messagesLimit = 10;
bool _hasMoreMessages = false;

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

if (Platform.isIOS) {
this._scrollController.addListener(() {
if (this._scrollController.position.pixels >= this._scrollController.position.maxScrollExtent + 150.0) {
this._checkForOldMessages();
}
});
}
}

@override
Widget build(BuildContext context) {
Widget w = WillPopScope(
child: Stack(
children: <Widget>[
Column(
children: <Widget>[
Expanded(child: this._streamBuilderWidget()),
// …
],
),
],
),
onWillPop: () {});


}

Widget _streamBuilderWidget() {
return StreamBuilder<List<Message>>(
initialData: this._messages,
stream: APIs().chats.messagesStream(chatID: widget.chat.chatID, orderBy: 'createdAt', descending: true, startAfter: this._startAfter, limit: this._messagesLimit),
builder: (context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
{
return PAIndicator();
}
default:
{
if (snapshot.data.length < this._messagesLimit) {
this._hasMoreMessages = false;
} else {
this._hasMoreMessages = true;
}

snapshot.data.forEach((m) {
print(m.message);
});

if (!ListEquality().equals(this._messages, snapshot.data)) {
snapshot.data.forEach((m) {
this._messages.add(m);
});
} else {
print('nonononono');
}

// Doesn't Work...
// if (mounted) this._listKey.currentState.insertItem(this._messages.length - 1, duration: Duration(milliseconds: 500));

return Platform.isIOS
? MessagesList(mKey: this._listKey, scrollController: this._scrollController, messages: this._messages, aUser: widget.aUser)
: RefreshIndicator(
child: MessagesList(mKey: this._listKey, scrollController: this._scrollController, messages: this._messages, aUser: widget.aUser),
onRefresh: () async {
await Future.delayed(Duration(seconds: 1));

this._checkForOldMessages();

return null;
});
}
}
});
}

_checkForOldMessages() {
if (this._hasMoreMessages) {
print('Adding More...');
setState(() {
this._startAfter = [this._messages.last.createdAt.toDate()];
});
this._streamBuilderWidget();
}
}

关于listview - Flutter/Firestore - 在 streambuilder 中的动态 ListView 上实现 "pagination",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56531427/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com