Consider the following build()
function:
Widget build(BuildContext context){
return MaterialApp(
home: Scaffold(
body: Center(
child: ListView.builder(
itemCount: 6,
itemBuilder: (context, i){
if(numberTruthList[i]){
return ListTile(
title: Text("$i"),
);
}
},
),
)
),
);
}
If the numberTruthList is
List<bool> numberTruthList = [true, true, true, true , true, true];
then the output comes out to be
and if the numberTruthList is
List<bool> numberTruthList = [false, true, true, true , true, true];
the output comes out to be
I want the output to be a ListView with the items
ListTile( title: Text("$i"),);
for values of i such that numberTruthList[i] is true, what should be the code?
This constructor is ListView.ListView. separated creates a fixed-length, scrollable , linear array of list “items” separated by list item “separators”. The itemBuilder callback is called whenever there are indices ≥ 0 and< itemCount .
Let us add a TextField to add a list item to the list dynamically. Run this application, and you should see a TextField widget and a Add button to add an item to the list dynamically. Type in some name and click on Add button. The item will be added at the top of the list dynamically.
ListView() constructor takes argument children where you can use map method of your list to create a list of widgets. These widgets are rendered immediately when the build method is called. ListView. builder() on the other hand, takes itemBuilder and itemCount parameters.
ListView.builder(
itemCount: 6,
itemBuilder: (context, i) {
return numberTruthList[i]
? ListTile(
title: Text(numberTruthList[i].toString()),
)
: Container(
height: 0,
width: 0,
);
},
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With