首页 > 解决方案 > 将方法移动到流构建器

问题描述

List<Photo> imgList = [];
  Future getCarouselWidget() async {
    var firestore = Firestore.instance;
    QuerySnapshot qn =
        await firestore.collection("history").getDocuments();
    List pics = qn.documents
        .map((it) => Photo(it['photo'].toString(), it['name'].toString(), it['address'].toString()))
        .toList();
    return imgList = pics;
  }

大家好,当我在我的数据库中进行更改时,我在我的应用程序中看不到它们?任何人都可以帮助我或指导我如何将其连接到流构建器或查询流

            StreamBuilder<QuerySnapshot>(
                stream: Firestore.instance
                    .collection('swimfinderlakes')
                    .snapshots(),
                builder: (context, snapshot) {

标签: flutterdart

解决方案


每次 Firestore 发生变化时,都会触发StreamBuilder. snapshot然后,您可以使用onbuilder方法访问最近的更新。然后您可以使用它来相应地更新用户界面。

        StreamBuilder<QuerySnapshot>(
            stream: Firestore.instance
                .collection('swimfinderlakes')
                .snapshots(),
            builder: (context, snapshot) {
             if (snapshot.connectionState == ConnectionState.active) {
               if (snapshot.hasData) {
                 List pics = qn.documents
                   .map((it) => Photo(it['photo'].toString(), 
                   it['name'].toString(), it['address'].toString())).toList();
                 return pics;
               }
             } else {
               return  CircularProgressIndicator();
             }
           }
            

推荐阅读