I am migrating my application from android to flutter and till now I have used ListView in a flutter. my question is, is there any specialized technique to handle a large amount of data in the flutter? for reference, you can look at android RecyclerView. it handles in-memory views and recycles its runtime. so how to achieve functionality like RecyclerView in Flutter? or it's not necessary for the flutter?
-
5use `ListView.builder` – pskink Nov 02 '18 at 08:17
-
2Flutter `ListView` is analog of ListView in android. `ListView.builder` is analog of RecyclerView. – Andrey Turkovsky Nov 02 '18 at 08:36
-
The [Write Your First Flutter App](https://flutter.io/get-started/codelab/) uses the `ListView.builder` as an example but you can get more information on [Working with long lists here](https://flutter.io/cookbook/lists/long-lists/) – SnakeyHips Nov 02 '18 at 08:43
3 Answers
The easiest way is to use a ListView.builder
without specifying the itemCount
parameter.
Here is the simplest example:
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text("Infinite List"),
),
body: ListView.builder(
itemBuilder: (context, index) {
return Text("$index");
},
),
);
}
}
Later, you can enhance this by fetching real data. You could show a 'CircularProgressIndicator' in the last item of the list while waiting for the new data.
body: ListView.builder(
itemBuilder: (context, index) {
if (index < data.length) {
// Show your info
Text("$index");
} else {
getMoreData();
return Center(child: CircularProgressIndicator());
}
},
itemCount: data.length + 1,
),
You can see that we trick the list by adding an index, and calling for more data when displaying that final index.
getMoreData()
would include a call to setState()
to force a rebuild and to take into account the new data.
![](../../users/profiles/2498179.webp)
- 13,342
- 3
- 40
- 41
-
I have added some explanation for an infinite list with real data. – chemamolins Nov 02 '18 at 10:20
Displaying lists of data is a fundamental pattern for mobile apps. Flutter includes the ListView widget to make working with lists a breeze.
I have solved the issue by doing the following steps
- Use the ListView Widget
- There are four constructors of ListView Class
- You have to use Builder Constructor (ListView.builder)
- Builder Constructor is used when you have to make a list of elements on demand
- It is appropriate for list views with a large (or infinite) number of children
HERE you can have Solution Video CLICK HERE
![](../../users/profiles/14758971.webp)
- 1
- 1
For infinite scroll ListView use infinite_scroll_pagination package this package manage a Lazily load and display pages of items as the user scrolls down screen.
Add dapendency
dependencies:
flutter:
sdk: flutter
infinite_scroll_pagination: ^2.3.0
Add an import to the new library at the top of the file:
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
Example of Uses
class CharacterListView extends StatefulWidget {
@override
_CharacterListViewState createState() => _CharacterListViewState();
}
class _CharacterListViewState extends State<CharacterListView> {
static const _pageSize = 20;
final PagingController<int, CharacterSummary> _pagingController =
PagingController(firstPageKey: 0);
@override
void initState() {
_pagingController.addPageRequestListener((pageKey) {
_fetchPage(pageKey);
});
super.initState();
}
Future<void> _fetchPage(int pageKey) async {
try {
final newItems = await RemoteApi.getCharacterList(pageKey, _pageSize);
final isLastPage = newItems.length < _pageSize;
if (isLastPage) {
_pagingController.appendLastPage(newItems);
} else {
final nextPageKey = pageKey + newItems.length;
_pagingController.appendPage(newItems, nextPageKey);
}
} catch (error) {
_pagingController.error = error;
}
}
@override
Widget build(BuildContext context) =>
// Don't worry about displaying progress or error indicators on screen; the
// package takes care of that. If you want to customize them, use the
// [PagedChildBuilderDelegate] properties.
PagedListView<int, CharacterSummary>(
pagingController: _pagingController,
builderDelegate: PagedChildBuilderDelegate<CharacterSummary>(
itemBuilder: (context, item, index) => CharacterListItem(
character: item,
),
),
);
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
}
![](../../users/profiles/10563627.webp)
- 14,668
- 7
- 90
- 90