strong textI use the below code to Create A long list view using Listview.builder() method but i don't understand where context, and index come from inside itembuilder(context, index)
In you can see the code below Listview.builder() returns a list of ListTile(), by using itemBuilder context, index itemBuilder:(context,index) but where this context and index is come from.. please help??
Here is the code
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(
title: "loglist",
home: Scaffold(
appBar: AppBar(
title: const Text("longlist"),
centerTitle: true,
),
body: getlonglist(),
),
));
}
List<String> stringlist = List<String>.generate(50, (items) {
return "item $items";
});
Widget getlonglist() {
Widget listitembuilder = ListView.builder(
**itemBuilder: (context, index)** {
return ListTile(
title: Text(stringlist[index]),
);
});
return listitembuilder;
}
CodePudding user response:
Try below code just add itemCount: stringlist.length,
inside your ListView
refer documentation for long lists
CodePudding user response:
This is how the flutter has created the listview.builder constructor. itemBuilder is a required arg which is of type IndexWidgetBuilder which requires an index and a buildcontext.
In order to let the itembuilder know how many items are present use itemCount