I would like to implement Firebase's Infinite scrolling through the "firebase_ui_firestore" library. The goal would be to load 12 documents initially and load more once the user reaches the end. However, the problem is that all documents from the collection are loaded and displayed once the screen is opened.
Could anyone help me with fixing the issue?
My code is the following:
import 'package:flutter/material.dart';
import 'package:firebase_ui_firestore/firebase_ui_firestore.dart';
import 'package:myapp/constants.dart';
import 'package:myapp/models/video_tile.dart';
import 'package:myapp/widgets/videos/profile_video_tile.dart';
class InfiniteScrollingScreen extends StatefulWidget {
final String userUid;
const InfiniteScrollingScreen({required this.userUid, Key? key}) : super(key: key);
@override
State<InfiniteScrollingScreen> createState() => _InfiniteScrollingScreenState();
}
class _InfiniteScrollingScreenState extends State<InfiniteScrollingScreen> {
@override
Widget build(BuildContext context) {
final query = firestore
.collection(videosPath)
.where('uid', isEqualTo: widget.userUid)
.withConverter(
fromFirestore: (snapshot, _) => VideoTile.fromJson(snapshot),
toFirestore: (user, _) => user.toJson(),
);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: FirestoreQueryBuilder<VideoTile>(
query: query,
pageSize: 12,
builder: (context, snapshot, _) {
if (snapshot.isFetching) {
return const Center(child: CircularProgressIndicator(),);
} else if (snapshot.hasError) {
return Text("Something went wrong! ${snapshot.error}");
} else {
return GridView.builder(
itemCount: snapshot.docs.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 8.0,
mainAxisSpacing: 8.0,
childAspectRatio: 1.0,
),
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
if (snapshot.hasMore && index + 1 == snapshot.docs.length) {
snapshot.fetchMore();
}
final videoTile = snapshot.docs[index].data();
return ProfileVideoTile(
thumbnailUrl: videoTile.thumbnail,
videoId: videoTile.id,
);
},
);
}
},
),
);
}
}
I already tried to fix the issue by removing "shrinkWrap: true", a SingleChildScrollView widget, a SafeArea widget, and a CupertinoPageScaffold widget. I don't know how to approach this problem otherwise.
I would appreciate your help!
Thanks in advance!