如何使用 StreamBuilder 将 firebase firestore 中的数据检索到 flutter 中并使用 ListView.builder 来显示值?

How to retrieve data from firebase firestore into flutter using StreamBuilder and also used ListView.builder to show the values?

如果我降级它的版本,但我在使用以下版本时遇到问题。

cloud_firestore: ^1.0.0

firebase_core: ^1,0.0

和flutter sdk版本:2.0.1

我正在尝试实现代码。 streamSnapshot.data.length 无效

body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('chats/UMrLSClddfvaw9wV0hs6/messages')
            .snapshots(),
        builder: (BuildContext context, AsyncSnapshot<dynamic> streamSnapshot) =>
            streamSnapshot.connectionState == ConnectionState.none
                ? Center(
                    child: CircularProgressIndicator(),
                  )
                : ListView.builder(
                    itemCount: streamSnapshot.data,
                    itemBuilder: (context, index) => Container(
                      padding: EdgeInsets.all(8),
                      child: Text('This works'),
                    ),
                  ),
      ),

改变这个:

builder: (BuildContext context, AsyncSnapshot<dynamic> streamSnapshot) =>

进入这个:

builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> streamSnapshot) =>

然后在itemCount你可以做:

itemCount: streamSnapshot.data.docs.length