Richard Ndumu
Richard Ndumu

Reputation: 13

add an item to ListView.builder flutter firebase

I'm trying to add a header item at the beginning of my list, so here is how I build it using firebase

new Expanded(
            child: new StreamBuilder(
                stream: Firestore.instance
                    .collection("users")
                    .document("dana")
                    .collection("Channels")
                    .snapshots(),
                builder: (context, snapshot) {
                  return new ListView.builder(
                    scrollDirection: Axis.vertical,
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (context, index) => _buildList(
                        context, snapshot.data.documents[index]),
                  );
                }),
          )

"_buildList" is just the Widget

Widget _buildListItem(BuildContext context, DocumentSnapshot document) 

so I'm basically lost, I've no idea how to add another Widget as header any suggestions?

Upvotes: 1

Views: 5207

Answers (1)

Bostrot
Bostrot

Reputation: 6033

You could return the "header" widget (the one you want above the listview but still in it) when the list is on index 0:

return new ListView.builder( 
    scrollDirection: Axis.vertical, 
    itemCount: snapshot.data.documents.length, 
    itemBuilder: (context, index) { 
        if (index == 0) {
            return someWidget, // return the widget you want as "header" here
        } else {
           return _buildList( context, snapshot.data.documents[index-1]), 
        }
    }
);

Upvotes: 2

Related Questions