未为“QuerySnapshot”类型定义 getter“documents”

时间:2021-02-18 13:32:24

标签: flutter dart

试图从 Firestore 实时数据库访问数据,但我一直收到这条消息和其他几条消息。在这种情况下如何访问数据?提前致谢

class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      FirestoreCrud firestoreCrudRef = new FirestoreCrud();
      Stream<QuerysnapShop> blogsSnapShot;
    
      @override
      void initState() {
        firestoreCrudRef.readData().then((result) {
          blogsSnapShot = result;
        });
        super.initState();
      }
    
      Widget blogList() {
        return Container(
          child: StreamBuilder<QuerysnapShop>(
              stream: blogsSnapShot,
              builder: (context, snapshot) {
                return ListView.builder(
                  scrollDirection: Axis.vertical,
                  physics: ScrollPhysics(),
                  padding: EdgeInsets.only(top: 24),
                  itemCount: snapshot.data.documents.length,
                  shrinkWrap: true,
                  itemBuilder: (context, index) {
                    return BlogTile(
                      author: snapshot.data.documents[index].data()['author'],
                      title: snapshot.data.documents[index].data()['tile'],
                      body: snapshot.data.documents[index].data()['body'],
                      imageUrl: snapshot.data.documents[index].data()['imageUrl'],
                    );
                  },
                );
              }),
        );
      }

3 个答案:

答案 0 :(得分:1)

SELECT COUNT(*) FROM Actions WHERE DATE = '01-02-2019' AND action_type = 'video starts'; 类有一个 QuerySnapshot 属性,然后返回一个 docs。然后,您可以从此 List 访问一个索引,该索引为您提供一个 List<QueryDocumentSnapshot>,然后您可以访问它的 QueryDocumentSnapshot 方法以获得一个 .data()。最后,您可以从中访问任何字段。

试试:

Map<String, dynamic>

答案 1 :(得分:0)

下面的代码应该可以帮助你达到预期的结果。

ListView.builder(
  scrollDirection: Axis.vertical,
  physics: ScrollPhysics(),
  padding: EdgeInsets.only(top: 24),
  itemCount: snapshot.data.docs.length,
  shrinkWrap: true,
  itemBuilder: (context, index) {
    var temp = snapshot.data.docs[index].data();
    return BlogTile(
      author: temp['author'],
      title: temp['tile'],
      body: temp['body'],
      imageUrl: temp['imageUrl'],
    );
  },
);

答案 2 :(得分:0)

preet shah 的解决方案不再有效。以下对我有用:

 return ListView.builder(
              itemCount: snapshot.data.docs.length,
              shrinkWrap: true,
              itemBuilder: (context, index) {
                var temp = snapshot.data.docs[index];
                return Container(
                  padding: EdgeInsets.all(10.0),
                  child: Card(
                    child: Text(temp['full_name']),
                  ),
                );
              },
            );