When using FirebaseAnimatedList
to render a list of message widgets in flutter, how would I implement an infinite scrolling mechanism that would load 10 new messages as the user scrolled up? This is the code for the FirebaseAnimatedList
and below are some methods I've tried using
FirebaseAnimatedList(
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
controller: _scrollController,
query: FirebaseDatabase.instance
.ref("messages/${widget.placeID}/")
.orderByChild("timeStamp"),
itemBuilder: (context, snapshot, animation, index) {
final json = snapshot.value as Map<dynamic, dynamic>;
final message = Message.fromJson(json);
return MessageWidget(
message: message.text,
id: message.uid,
name: message.name,
lastSender: message.lastSender,
date: message.timeStamp,
);
},
);
}
Code in initState:
int scrollValue = 20;
var messagesQuery;
final ScrollController _scrollController = ScrollController();
@override
void initState() {
setState(() {
_scrollController.hasClients == true;
messagesQuery = FirebaseDatabase.instance
.ref("messages/ChIJu9mKv73Bt4kRu9BSvbTPfDU/")
.orderByChild("timeStamp")
.limitToLast(scrollValue);
});
_tabController = TabController(length: 3, vsync: this);
_scrollController.addListener(() {
if (_scrollController.position.atEdge) {
bool isTop = _scrollController.position.pixels == 0;
if (isTop) {
setState(() {
scrollValue = 20;
messagesQuery = FirebaseDatabase.instance
.ref("messages/ChIJu9mKv73Bt4kRu9BSvbTPfDU/")
.orderByChild("timeStamp")
.limitToLast(scrollValue + 10);
});
} else {
print('At the bottom');
}
}
});
super.initState();
}
The code above doesn't add 20 more messages, nothing happens. I know it detects when the list has been scrolled to the top since it outputs the correct print
message, but it doesn't updated the list to include more messages.