Introduction
In this article, we are going to learn about pagination in Flutter using Firebase Cloud Firestore database. As we know that accessing data in small pieces rather than the whole bunch is a better approach in terms of application performance and bandwidth cost, I would like to include that Firebase Firestore bills you according to the number of reads, writes, and delete operations so, this article will be very crucial to those developers who want to use Firebase Cloud Firestore database as backend for their app.
We are going to implement the scroll to load feature for loading more data in the below example. Initially, we will load 15 records and then after, whenever the user reaches the end of the list or bottom of the list, we will load another 15 records and so on. So, let’s begin our step by step example tutorial for pagination in Flutter using Firebase Cloud Firestore database.
Prerequisites
- Created New Project named as “flutter_firebase_pagination”.
- I am assuming that you are familiar with how to create a Firebase project and initialize the Firestore database in it. If you are a beginner and don’t have an idea about Firestore, you can check my article Firebase Firestore CRUD Operation In Flutter.
- After creating the Firebase database, create a collection named “products” and add 25 documents manually in products collection. I have attached a screenshot that will make you easily understand how to structure our collection. There are two fields in the document - “name” and “short_desc”.

Programming Steps
Step 1
Open the pubspec.yaml file in your project and add the following dependencies into it.
- dependencies:
- flutter:
- sdk: flutter
- cupertino_icons: ^0.1.2
- cloud_firestore: ^0.12.7
Step 2
Open the main.dart file and import Firestore package.
- import 'package:cloud_firestore/cloud_firestore.dart';
Step 3
Initialize the Firestore instance in the state class.
- Firestore firestore = Firestore.instance;
Step 4
Define another variable for storing data and tracking the load progress.
- List<DocumentSnapshot> products = []; // stores fetched products
- bool isLoading = false; // track if products fetching
- bool hasMore = true; // flag for more products available or not
- int documentLimit = 10; // documents to be fetched per request
- DocumentSnapshot lastDocument; // flag for last document from where next 10 records to be fetched
- ScrollController _scrollController = ScrollController(); // listener for listview scrolling
Step 5
Define the getProducts() function to get initial 10 documents.

Join the conversation! Your thoughts help the community grow.