addPageRequestListeneris called indefinitely. It fetches the datas for next pages without having to scroll
Maybe I’m doing something wrong, but I don’t know what.
`
class PostPage extends StatefulWidget {
const PostPage({super.key});
@override
PostState createState() => PostState();
}
class PostState extends State<PostPage> {
final FirebaseFirestore fireStore = FirebaseFirestore.instance;
final String currentUserId = FirebaseAuth.instance.currentUser!.uid;
static const _pageSize = 10;
final PagingController<DocumentSnapshot?, DocumentSnapshot>
_pagingController = PagingController(firstPageKey: null);
@override
void initState() {
super.initState();
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
}
Future<void> _fetchPage(DocumentSnapshot? lastDocument) async {
try {
final query = fireStore
.collection("Posts")
.orderBy("dateTime", descending: true)
.limit(_pageSize);
final snapshot = lastDocument == null
? await query.get()
: await query.startAfterDocument(lastDocument).get();
await Future.delayed(const Duration(seconds: 1));
final isLastPage = snapshot.docs.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(snapshot.docs);
} else {
final nextPageKey = snapshot.docs.last;
_pagingController.appendPage(snapshot.docs, nextPageKey);
}
} catch (error) {
_pagingController.error = error;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Theme.of(context).scaffoldBackgroundColor,
appBar: AppBar(
title: Text(
'자유게시판',
style: Theme.of(context)
.textTheme
.bodyLarge!
.copyWith(color: Colors.black87),
),
iconTheme: IconThemeData(color: Theme.of(context).primaryColor),
backgroundColor: Theme.of(context).appBarTheme.backgroundColor,
bottom: PreferredSize(
preferredSize: const Size.fromHeight(0.0),
child: Container(
color: Colors.grey.shade200,
height: 1.0,
),
),
),
body: RefreshIndicator(
onRefresh: () => Future.sync(() {
_pagingController.refresh();
loadBlockedUsers();
}),
child: PagedListView<DocumentSnapshot?, DocumentSnapshot>(
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<DocumentSnapshot>(
itemBuilder: (context, item, index) {
if (_blockedUsers.contains(item['uid'])) {
return const SizedBox.shrink(); // 차단된 사용자의 게시물은 표시하지 않습니다.
}
return (item['image'] != null
? item['avatarUrl'] == null
? (FeedPageBody(
uid: item['uid'],
name: item['name'],
content: item['contents'],
photoUrls: item['image'],
dateTime: item['dateTime'],
documentId: item.id,
currentUserId: currentUserId,
anoym: item['anoym'],
commentsCount: item['commentsCount'],
))
: (FeedPageBody(
uid: item['uid'],
name: item['name'],
content: item['contents'],
photoUrls: item['image'],
dateTime: item['dateTime'],
documentId: item.id,
currentUserId: currentUserId,
anoym: item['anoym'],
commentsCount: item['commentsCount'],
avatarUrl: item['avatarUrl'],
))
: item['avatarUrl'] == null
? (FeedPageBody(
uid: item['uid'],
name: item['name'],
content: item['contents'],
dateTime: item['dateTime'],
documentId: item.id,
currentUserId: currentUserId,
anoym: item['anoym'],
commentsCount: item['commentsCount'],
))
: (FeedPageBody(
uid: item['uid'],
name: item['name'],
content: item['contents'],
dateTime: item['dateTime'],
documentId: item.id,
currentUserId: currentUserId,
anoym: item['anoym'],
commentsCount: item['commentsCount'],
avatarUrl: item['avatarUrl'],
)));
},
noItemsFoundIndicatorBuilder: (context) => const Center(
child: Text("표시할 게시물이 없어요", style: TextStyle(fontSize: 20)),
),
),
),
),
floatingActionButton: floatingButtons(),
);
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
}
`
Do you know what is happening?
Thank you for your help!
I tried CustomScrollView but it did not work
New contributor
김성욱 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.