0
votes

i want to get all document in flutter list view.. so i do this...

class AudioScreenList extends StatefulWidget {
  @override
  _AudioScreenListState createState() => _AudioScreenListState();
}

class _AudioScreenListState extends State<AudioScreenList> {
  bool isPerformingRequest = false;

  static const String _collectionRussian = 'users';
  static const String _loadingTextRussian = 'Loading...';
  static const String _speechTitle = 'full_name';
  static const String _speechSubtitle = 'coins';
  static const String _audioLink = 'audioLink';
  static const String _pastorCode = 'pastorCode';
  static const String _pastorName = 'coins';
  static const String _id = "id";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection(_collectionRussian)
            // .orderBy(_id, descending: true)
            .snapshots(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (!snapshot.hasData)
            return const Center(
                child: Text(
              _loadingTextRussian,
              style: TextStyle(fontSize: 25.0, color: Colors.grey),
            ));
          return ListView(children: getExpenseItems(snapshot));
        },
      ),
    );
  }

  getExpenseItems(AsyncSnapshot<QuerySnapshot> snapshot) {
    // ignore: deprecated_member_use
    return snapshot.data.documents
        .map(
          (doc) => Container(
            margin: const EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 0.0),
            child: ListTile(
                leading: Container(
                  width: 70.0,
                  height: 70.0,
                  child: Image.asset(
                    "assets/profile.png",
                    fit: BoxFit.cover,
                  ),
                ),
                title: Text(doc[_speechTitle]),
                subtitle: Text(
                  doc[_speechSubtitle].toString(),
                ),
                onTap: () => {}),
          ),
        )
        .toList();
  }
}

how i get Document id in from StreamBuilder? when click on this button open another list & detect the click document list enter image description here

i tried this but not working....

snapshot.data.doc['index']

i want to get document id when click on the list item..

1

1 Answers

1
votes

You're asking two different things. The title of your question is about determining the index of each document in the map() function, and you can find an answer here: Flutter - Get iteration index from List.map()

The other question if about finding the ID of each document, which you can do with doc.id inside the loop. So for example:

return snapshot.data.documents
    .map(
      (doc) => Container(
        margin: const EdgeInsets.fromLTRB(0.0, 8.0, 0.0, 0.0),
        child: ListTile(
            ...,
            onTap: () => { print(doc.id) }),
      ),