Black Eyed Beans
Black Eyed Beans

Reputation: 259

Flutter Listview not displaying all items

I have a listview that is supposed to display a stream of items from firebase. After every two items there is a custom widget that am displaying and to achieve this am using if else statement. The problem now is that my custom widget is overwriting some of the items from firebase such that if there are 5 total items in the stream, am only getting 3 and the other 2 are being replaced by my custom widget. Is there a way to get all the items including the custom widget so that (following the example above) I can have 7 total items? Here is my code for reference

StreamBuilder(
            stream: stream,
            //stream: getposts(),
            builder: (BuildContext context,
                AsyncSnapshot<List<DocumentSnapshot>>snapshots) {
return Flexible(
                  child: ListView.builder(
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemBuilder: (context, index) {

                    if(index % 2 == 0 && index != 0){
                       return Container(child: Text("Custom Widget")) //this is the code inserting custom widget into the listview
                    else{ Container(child: Text(snapshots.data.title))
}

}))
}

Upvotes: 1

Views: 901

Answers (1)

Gpack
Gpack

Reputation: 2193

Could you use a Column like so(?)

if(index % 2 == 0 && index != 0){
  return Column(
    children: [
      Container(child: Text("Custom Widget")),
      Container(child: Text(snapshots.data.title)),
    ]
  );
} else {
  return Container(child: Text(snapshots.data.title))
}

Upvotes: 1

Related Questions