Flutter如何使用StreamBuilder中的数据更新ListView

时间:2020-07-01 13:26:25

标签: firebase flutter dart google-cloud-firestore stream-builder

我有一个带有streambuilder的小部件,可从firebase获取数据。我想在另一个小部件中的列表视图中显示此数据,但它不会显示在列表视图中。这是我的代码:

Widget _buildStream(BuildContext context){
return StreamBuilder(
          stream: Firestore.instance.collection('markers').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData)
              return Center(
                child: CircularProgressIndicator(),
              );
            for (int i = 0; i < snapshot.data.documents.length; i++) {
               productMap.add(snapshot.data.documents[i]['name']);
               
            }
            return _buildGoogleMap(context);
}
);}

另一个带有列表视图的小部件:

Widget _panel(ScrollController sc){
return MediaQuery.removePadding(
  context: context,
  removeTop: true,
  child: ListView(
    controller: sc,
    children: <Widget>[
      SizedBox(height: 12.0,),

      Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            width: 30,
            height: 5,
            decoration: BoxDecoration(
            color: Colors.grey[300],
              borderRadius: BorderRadius.all(Radius.circular(12.0))
            ),
          ),
        ],
      ),
      SizedBox(height: 18.0,),
      ListView.builder(
                          primary: false,
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                          itemCount:
                              productMap == null ? 0 : productMap.length,
                          itemBuilder: (BuildContext context, int index) {
                            return new GestureDetector(
                              onTap: () {
                              },
                              child: Card(
                                  child: ListTile(
                                leading: CircleAvatar(
                                  backgroundImage:
                                      AssetImage('asset/green.png'),
                                ),
                                title: Text(productMap[index]),
                              )),
                            );
                          },
                        )
    ],
  )
);
}

即使productMap包含数据,列表视图也为空。我该怎么办?

0 个答案:

没有答案