Home > Back-end >  I created a long List view with Listview.Builder() Widget Sucessfully but where context and index co
I created a long List view with Listview.Builder() Widget Sucessfully but where context and index co

Time:11-26

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 enter image description here

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

  • Related