List works fine but at the end of items which is 49 in my case i want to stop scrolling, but it goes down and shows error. How to fix?
Code:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: getListView(),
)
);
}
}
List<String> getelements(){
var items = List<String>.generate(50, (index) => 'Item $index');
return items;
}
Widget getListView(){
var listitem = getelements();
var listview = ListView.builder(
itemBuilder: (context, index){
return ListTile(
title: Text(listitem[index]),
);
},
);
return listview;
}
Regards,
好的,现在只需添加itemCount: 50,
The issue is that you need to mention the itemCount
to the ListView
widget.
itemCount: listitem.length
So your widget should look like:
var listview = ListView.builder(
itemBuilder: (context, index){
return ListTile(
title: Text(listitem[index]),
);
},
itemCount: listitem.length,
);
Also you should consider removing the getElements()
and not create all the elements of the list initially, and rather use the builder to create elements when on-demand when needed:
var listview = ListView.builder(
itemBuilder: (context, index){
return ListTile(
title: Text('Item $index'),
);
},
itemCount: 50,
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.