Flutter - 如何获取快照的索引值?

Flutter - how can get the index value of snapshots?

我想在 flutter 列表视图中获取所有文档..所以我这样做...

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();
  }
}

如何从 StreamBuilder 获取文档 ID? 单击此按钮时打开另一个列表并检测单击文档列表 enter image description here

我试过了但是没用....

snapshot.data.doc['index']

我想在单击列表项时获取文档 ID..

你问的是两件不同的事情。你的问题标题是关于map()函数中每个文档的index的判断,你可以在这里找到答案:Flutter - Get iteration index from List.map()

另一个问题是关于查找每个文档的 ID,您可以在循环中使用 doc.id 来完成。例如:

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) }),
      ),