0
votes

I'm new to flutter and I'm trying to pass a firestore document snapshot to another class. I passed to the Profile class a snapshot document, and I want to indicate the index of my document, but I don't know how to get it I have this

                Expanded(
                child: StreamBuilder<QuerySnapshot>(
                    stream: ((searchString != null) &&
                            (searchString.trim() != ""))
                        ? Firestore.instance
                            .collection('pazienti')
                            .where("searchIndex",
                                arrayContains: searchString)
                            .snapshots()
                        : Firestore.instance
                            .collection('pazienti')
                            .snapshots(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (snapshot.hasError)
                        return Text('Error: ${snapshot.error}');
                      switch (snapshot.connectionState) {
                        case ConnectionState.waiting:
                          return Center(child: CircularProgressIndicator());
                        default:
                          return ListView(
                            children: snapshot.data.documents
                                .map((DocumentSnapshot document) {

                              return Card(

                                elevation: 10.00,
                                margin: EdgeInsets.all(0.50),
                                child: ListTile(
                                  onTap: () {
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => Profile(miaquery: snapshot.data.documents[????])));
                                  } 
                                  ,
                                  leading: CircleAvatar(
                                    backgroundColor:
                                        Colors.blueGrey.shade800,
                                  ),
                                  title: Text(document['cognome'] +
                                      " " +
                                      document['nome']),
                                  subtitle: Text(document['cognome'] +
                                      " " +
                                      document['nome']),
                                   ),
                              );

                            }).toList(),
                          );
                      }
                    })),
          ],
        ),
      )

My problem is essentially here

Navigator.push(context, MaterialPageRoute(builder: (context) => Profile(miaquery: snapshot.data.documents[XXXX]))

How can I get the index of the document from the map I used?

Thank you very much for your help

4
Profile(miaquery: document ) try this - Viren V Varasadiya

4 Answers

1
votes

You just want to pass document on which tap, so you can simply pass document which you are getting from map method.

0
votes

Snapshots from a query don't have a numeric index. The results from a query could change at any time between queries, and the system can not guarantee that any sort of index would be stable.

If you want to pass a document to another function, pass its unique document ID. The receiver can then query the document directly, perhaps from local cache without requiring a billed read operation at the server.

0
votes
var listIndex= snapshot.data.documents.map((e) => e.data['key']);
var passIndex = listIndex.toList().indexOf(doc.data['key']);
print(passIndex);
0
votes

You can simply pass the index when assigning the list

Widget _buildList(BuildContext context, List<DocumentSnapshot> snapshot) {
    return ListView(
      padding: const EdgeInsets.only(top: 20.0),
      children: snapshot.map((data) => _buildItem(context, data, snapshot.indexOf(data))).toList(),
    );
}