Education
Software Development Executive - I
Software Development Executive - II
Last updated on Sep 29, 2023
Last updated on Sep 13, 2023
In the world of app development, Flutter has emerged as a game-changer. It's a UI toolkit from Google, designed to create natively compiled applications for mobile, web, and desktop from a single codebase. One of the key features that make Flutter stand out is its ability to handle infinite lists.
Infinite lists, or as some may refer to it, infinite scroll pagination, is a technique where data is loaded continuously as the user scrolls down the screen. This method of lazy loading pagination is commonly used in applications that display pages of content, enhancing the user experience by providing a seamless flow of data.
Infinite lists in Flutter are a way to display paginated data efficiently. When a user scrolls to the end of the current page, more data is fetched and added to the list. This is also known as endless scrolling pagination.
The process begins with the BuildContext context and the creation of a class MyApp extends StatelessWidget. This class is where we set up the basic structure of our app. Next, we create a class MyHomePage extends StatefulWidget, which will manage the state of our infinite list.
As the user scrolls, we fetch new data and add it to the list. This is where the concept of infinite scroll pagination comes into play. The infinite scrolling technique involves the use of a loading indicator. This indicator shows progress as more data is fetched and loaded onto the screen.
Infinite lists are a great way to handle large amounts of data. They allow us to load and display pages of data efficiently, without overloading the user's screen or consuming too much memory. This is achieved through progressive loading pagination, where data is loaded in chunks as the user scrolls.
The ListView widget is a fundamental part of creating lists in Flutter. It's a scrollable, linear list of widgets that are arranged vertically. ListView is perfect for displaying a small to medium number of children. It creates a scrollable list and automatically removes widgets that are further away from the viewport.
Here is a basic example of how to create a ListView:
1 void main() { 2 runApp(MyApp()); 3 } 4 5 class MyApp extends StatelessWidget { 6 @override 7 Widget build(BuildContext context) { 8 return MaterialApp( 9 home: Scaffold( 10 appBar: AppBar( 11 title: Text('Infinite List Example'), 12 ), 13 body: ListView( 14 children: <Widget>[ 15 ListTile( 16 leading: Icon(Icons.map), 17 title: Text('Map'), 18 ), 19 ListTile( 20 leading: Icon(Icons.photo_album), 21 title: Text('Album'), 22 ), 23 ListTile( 24 leading: Icon(Icons.phone), 25 title: Text('Phone'), 26 ), 27 ], 28 ), 29 ), 30 ); 31 } 32 } 33
To create a basic static list in Flutter, we start by defining a class MyApp extends StatelessWidget. This class will return a MaterialApp widget that contains our list. Inside this class, we create a class MyHomePage extends StatefulWidget that will manage the state of our list.
Here is an example of how to create a basic static list:
1 void main() { 2 runApp(MyApp()); 3 } 4 5 class MyApp extends StatelessWidget { 6 @override 7 Widget build(BuildContext context) { 8 return MaterialApp( 9 home: Scaffold( 10 appBar: AppBar( 11 title: Text('Static List Example'), 12 ), 13 body: ListView.builder( 14 itemCount: 20, 15 itemBuilder: (BuildContext context, int index) { 16 return ListTile( 17 title: Text('Item ${index + 1}'), 18 ); 19 }, 20 ), 21 ), 22 ); 23 } 24 } 25
In this code snippet, we use the ListView.builder constructor, which is more appropriate for lists with a large (or infinite) number of children because it creates the widgets on demand. The itemBuilder function is called only for those items that are actually visible.
Pagination is a technique used in applications that deal with large amounts of data. Instead of loading all the data at once, which can be overwhelming and resource-intensive, pagination breaks the data into smaller, manageable chunks, or pages. When a user scrolls to the end of the current page, more data is fetched and displayed. This is known as infinite scroll pagination or auto pagination.
Infinite lists in Flutter use this concept of pagination to efficiently handle large amounts of data. As the user scrolls, new data is fetched and added to the list. This process is repeated until all the data has been loaded and displayed. This technique of loading and displaying pages of data as the user scrolls is also known as lazy loading pagination or progressive loading pagination.
Implementing infinite scrolling in Flutter involves creating a ListView and using a ScrollController to determine when the user has scrolled to the end of the list. When the user reaches the end, more data is fetched and added to the list.
Here is an example of how to implement infinite scrolling in Flutter:
1 void main() { 2 runApp(MyApp()); 3 } 4 5 class MyApp extends StatelessWidget { 6 @override 7 Widget build(BuildContext context) { 8 return MaterialApp( 9 home: MyHomePage(), 10 ); 11 } 12 } 13 14 class MyHomePage extends StatefulWidget { 15 @override 16 _MyHomePageState createState() => _MyHomePageState(); 17 } 18 19 class _MyHomePageState extends State<MyHomePage> { 20 final _scrollController = ScrollController(); 21 final _list = List.generate(20, (index) => 'Item ${index + 1}'); 22 int _currentPage = 1; 23 24 @override 25 void initState() { 26 super.initState(); 27 _scrollController.addListener(_loadMore); 28 } 29 30 @override 31 void dispose() { 32 _scrollController.dispose(); 33 super.dispose(); 34 } 35 36 void _loadMore() { 37 if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) { 38 setState(() { 39 _currentPage++; 40 _list.addAll(List.generate(20, (index) => 'Item ${index + 1 + _currentPage * 20}')); 41 }); 42 } 43 } 44 45 @override 46 Widget build(BuildContext context) { 47 return Scaffold( 48 appBar: AppBar( 49 title: Text('Infinite Scrolling Example'), 50 ), 51 body: ListView.builder( 52 controller: _scrollController, 53 itemCount: _list.length, 54 itemBuilder: (BuildContext context, int index) { 55 return ListTile( 56 title: Text(_list[index]), 57 ); 58 }, 59 ), 60 ); 61 } 62 } 63
In this code snippet, we create a ScrollController and attach a listener to it. The listener calls the loadMore function when the user has scrolled to the end of the list. The loadMore function fetches more data and adds it to the list.
Fetching data from an API is a common task in modern applications. In Flutter, we can use the http package to send HTTP requests and fetch data from an API.
Here is an example of how to fetch data from an API in Flutter:
1 import 'package:http/http.dart' as http; 2 import 'dart:convert'; 3 4 void main() { 5 runApp(MyApp()); 6 } 7 8 class MyApp extends StatelessWidget { 9 @override 10 Widget build(BuildContext context) { 11 return MaterialApp( 12 home: MyHomePage(), 13 ); 14 } 15 } 16 17 class MyHomePage extends StatefulWidget { 18 @override 19 _MyHomePageState createState() => _MyHomePageState(); 20 } 21 22 class _MyHomePageState extends State<MyHomePage> { 23 Future<List<String>> _fetchData() async { 24 final response = await http.get('https://api.example.com/items'); 25 if (response.statusCode == 200) { 26 return List<String>.from(json.decode(response.body)); 27 } else { 28 throw Exception('Failed to load data'); 29 } 30 } 31 32 @override 33 Widget build(BuildContext context) { 34 return Scaffold( 35 appBar: AppBar( 36 title: Text('API Data Fetching Example'), 37 ), 38 body: FutureBuilder<List<String>>( 39 future: _fetchData(), 40 builder: (BuildContext context, AsyncSnapshot<List<String>> snapshot) { 41 if (snapshot.hasData) { 42 return ListView.builder( 43 itemCount: snapshot.data.length, 44 itemBuilder: (BuildContext context, int index) { 45 return ListTile( 46 title: Text(snapshot.data[index]), 47 ); 48 }, 49 ); 50 } else if (snapshot.hasError) { 51 return Center(child: Text('Error: ${snapshot.error}')); 52 } else { 53 return Center(child: CircularProgressIndicator()); 54 } 55 }, 56 ), 57 ); 58 } 59 } 60
To send a GET request to the API, we utilize the http.get function in this code snippet. Using the json.decode function, the response is then parsed into a list of strings. A FutureBuilder is used to display this list in a ListView.
API pagination is a technique used to break large amounts of data into smaller, more manageable chunks. This is especially useful when working with APIs that return a large amount of data.
In Flutter, we can implement API pagination by sending a page number with our API request. The server will then return only the data for that specific page. When the user scrolls to the end of the current page, we fetch and display the next page of data.
Here is an example of how to implement API pagination in Flutter:
1 import 'package:http/http.dart' as http; 2 import 'dart:convert'; 3 4 void main() { 5 runApp(MyApp()); 6 } 7 8 class MyApp extends StatelessWidget { 9 @override 10 Widget build(BuildContext context) { 11 return MaterialApp( 12 home: MyHomePage(), 13 ); 14 } 15 } 16 17 class MyHomePage extends StatefulWidget { 18 @override 19 _MyHomePageState createState() => _MyHomePageState(); 20 } 21 22 class _MyHomePageState extends State<MyHomePage> { 23 final _scrollController = ScrollController(); 24 final _list = <String>[]; 25 int _currentPage = 1; 26 27 @override 28 void initState() { 29 super.initState(); 30 _scrollController.addListener(_loadMore); 31 _fetchData(_currentPage); 32 } 33 34 @override 35 void dispose() { 36 _scrollController.dispose(); 37 super.dispose(); 38 } 39 40 Future<void> _fetchData(int pageKey) async { 41 final response = await http.get('https://api.example.com/items?page=$pageKey'); 42 if (response.statusCode == 200) { 43 setState(() { 44 _list.addAll(List<String>.from(json.decode(response.body))); 45 }); 46 } else { 47 throw Exception('Failed to load data'); 48 } 49 } 50 51 void _loadMore() { 52 if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent) { 53 _currentPage++; 54 _fetchData(_currentPage); 55 } 56 } 57 58 @override 59 Widget build(BuildContext context) { 60 return Scaffold( 61 appBar: AppBar( 62 title: Text('API Pagination Example'), 63 ), 64 body: ListView.builder( 65 controller: _scrollController, 66 itemCount: _list.length, 67 itemBuilder: (BuildContext context, int index) { 68 return ListTile( 69 title: Text(_list[index]), 70 ); 71 }, 72 ), 73 ); 74 } 75 } 76
When implementing infinite lists, it's important to handle loading and error states properly. This ensures a smooth user experience and helps prevent issues that can arise from network errors or slow connections.
When fetching new data, display a loading indicator at the end of the list. This gives the user feedback that more data is being loaded. Once the data is fetched and added to the list, remove the loading indicator.
If an error occurs while fetching data, display an error message to the user. This can be done using a Snackbar, a dialog, or a custom error widget.
1 import 'package:http/http.dart' as http; 2 import 'dart:convert'; 3 4 void main() { 5 runApp(MyApp()); 6 } 7 8 class MyApp extends StatelessWidget { 9 @override 10 Widget build(BuildContext context) { 11 return MaterialApp( 12 home: MyHomePage(), 13 ); 14 } 15 } 16 17 class MyHomePage extends StatefulWidget { 18 @override 19 _MyHomePageState createState() => _MyHomePageState(); 20 } 21 22 class _MyHomePageState extends State<MyHomePage> { 23 final _scrollController = ScrollController(); 24 final _list = <String>[]; 25 int _currentPage = 1; 26 bool _isLoading = false; 27 String _error; 28 29 @override 30 void initState() { 31 super.initState(); 32 _scrollController.addListener(_loadMore); 33 _fetchData(_currentPage); 34 } 35 36 @override 37 void dispose() { 38 _scrollController.dispose(); 39 super.dispose(); 40 } 41 42 Future<void> _fetchData(int pageKey) async { 43 setState(() { 44 _isLoading = true; 45 _error = null; 46 }); 47 try { 48 final response = await http.get('https://api.example.com/items?page=$pageKey'); 49 if (response.statusCode == 200) { 50 setState(() { 51 _list.addAll(List<String>.from(json.decode(response.body))); 52 _isLoading = false; 53 }); 54 } else { 55 throw Exception('Failed to load data'); 56 } 57 } catch (e) { 58 setState(() { 59 _isLoading = false; 60 _error = e.toString(); 61 }); 62 } 63 } 64 65 void _loadMore() { 66 if (_scrollController.position.pixels == _scrollController.position.maxScrollExtent && !_isLoading) { 67 _currentPage++; 68 _fetchData(_currentPage); 69 } 70 } 71 72 @override 73 Widget build(BuildContext context) { 74 return Scaffold( 75 appBar: AppBar( 76 title: Text('Infinite Scrolling Example with Loading and Error States'), 77 ), 78 body: _error != null 79 ? Center(child: Text('Error: $_error')) 80 : ListView.builder( 81 controller: _scrollController, 82 itemCount: _list.length + (_isLoading ? 1 : 0), 83 itemBuilder: (BuildContext context, int index) { 84 if (index == _list.length) { 85 return Center(child: CircularProgressIndicator()); 86 } else { 87 return ListTile( 88 title: Text(_list[index]), 89 ); 90 } 91 }, 92 ), 93 ); 94 } 95 } 96
In this code snippet, we use the isLoading variable to track whether new data is being loaded. If isLoading is true, we display a CircularProgressIndicator at the end of the list. If an error occurs, we display the error message in a Center widget.
When working with large lists, performance can become an issue. To ensure smooth scrolling and efficient memory usage, use the ListView.builder constructor to create your lists.
The ListView.builder constructor creates items as they scroll onto the screen, and discards items when they are scrolled out of view. This means that regardless of how large your list is, only a small number of widgets (those currently on screen) are in memory at any given time.
In addition, try to minimize the amount of work done in the itemBuilder function. This function is called every time a new item scrolls onto the screen, so any heavy computations or network requests in this function can cause scrolling to stutter.
Finally, if your list contains items of varying heights, make sure to specify the itemExtent property. This allows Flutter to calculate the scroll position more accurately and improve performance.
Infinite lists are a powerful feature in Flutter that allows us to handle large amounts of data efficiently. By loading and displaying pages of data as the user scrolls, we can provide a smooth and seamless user experience.
We've covered a lot of ground in this post, from understanding the concept of infinite lists and pagination to fetching data from an API and implementing infinite scrolling. We've also looked at best practices for handling loading and error states and optimizing performance for large lists. With these techniques in your toolkit, you're well-equipped to create dynamic and efficient lists in your Flutter apps. And there you have it - the infinite possibilities of infinite lists in Flutter, all at your fingertips.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.
Tired coding all day?
Do it with a few clicks.