'how to use for loop inside itemBuilder flutter
i am trying to use for loop inside itemBuilder to show inline ads, i have two ListView.separated one is list from data api and the second one from admob ads , my code is work fine and show the ads but it just show once because i used this line of code i want to convert it to for loop but i don't know how I just want to change this line of code
if (index == 10) { return _getAdWidget();}
i want to convert it to for loop and execute every 10 index please help
here is my code
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
automaticallyImplyLeading: true,
backgroundColor: Colors.black,
title: Text("طرائف عن الحيوانات"),
centerTitle: true,
),
backgroundColor: Colors.grey[900],
body: FutureBuilder(
future: getData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
List snap = snapshot.data;
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(
child: Text("error"),
);
}
return ListView.separated(
itemCount: snap.length,
separatorBuilder: (context, index) {
final data = snap[index];
final dataJoke = data["joke"];
final dataAnswer = data["answer"];
return Card(
elevation: 6,
margin: EdgeInsets.all(10),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/110.png"),
fit: BoxFit.cover,
),
),
child: ListTile(
title: Text(
" ${snap[index]['joke']}",
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
subtitle: Text(
" ${snap[index]['answer']}",
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w600),
),
trailing: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(1, 30),
primary: Colors.grey[900], // background
onPrimary: Colors.yellow,
elevation: 0),
onPressed: () async {
const urlPreview =
"https://www.youtube.com/watch?v=CNUBhb_cM6E";
await Share.share(
"$dataJoke \n $dataAnswer \n للمزيد من الطرائف\n$urlPreview");
},
child: Icon(Icons.share_outlined),
),
),
),
);
},
itemBuilder: (BuildContext context, int index) {
if (index == 10) {
return _getAdWidget();
}
Solution 1:[1]
Try this:
return ListView.builder(
itemCount: snap.length + (snap.length ~/ 10),
itemBuilder: (BuildContext context, int index) {
if (index % 10 == 0) {
return _getAdWidget();
}
final data = snap[index];
final dataJoke = data["joke"];
final dataAnswer = data["answer"];
return Card(
elevation: 6,
margin: EdgeInsets.all(10),
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("images/110.png"),
fit: BoxFit.cover,
),
),
child: ListTile(
title: Text(
" ${snap[index]['joke']}",
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
subtitle: Text(
" ${snap[index]['answer']}",
textDirection: TextDirection.rtl,
style: TextStyle(
fontSize: 18, fontWeight: FontWeight.w600),
),
trailing: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: Size(1, 30),
primary: Colors.grey[900], // background
onPrimary: Colors.yellow,
elevation: 0),
onPressed: () async {
const urlPreview =
"https://www.youtube.com/watch?v=CNUBhb_cM6E";
await Share.share(
"$dataJoke \n $dataAnswer \n ?????? ?? ???????\n$urlPreview");
},
child: Icon(Icons.share_outlined),
),
),
),
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
