将Firebase Firestore Listview数据显示为列表Flutter

我目前能够显示一个Listview,其中包含我的Firestore数据库中的数据 . 我目前的问题是,我想让它变得不可接受,所以我需要能够使用如下函数:

setState(() {
    items.removeAt(index);
  });

现在,我读到了如何生成列表,但没有一个示例提到像我正在使用的firebase Streambuilder . 所以我只是想知道是否可以将数据放入列表中?如果没有,如果还有其他方法可以使firestore listview无法解决?以下是我目前获取数据的方式:

Container(
          child: StreamBuilder(
            stream: Firestore.instance.collection('users').snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return Center(
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation<Color>(themeColor),
                  ),
                );
              } else {
                return ListView.builder(
                  scrollDirection: Axis.vertical,
                  padding: EdgeInsets.all(10.0),
                  itemBuilder: (context, index) => buildItem(context, snapshot.data.documents[index]),
                  itemCount: snapshot.data.documents.length,
                );
              }
            },
          ),
        ),

在此先感谢,任何帮助表示赞赏 .

Builditem看起来像这样:

Widget buildItem(BuildContext context, DocumentSnapshot document) {
if (document['id'] == currentUserId || document['gender'] == null) {
  return Container();
}
if (currentUserPreference == 'male' && currentUserGender == 'male') {
  return showGayMales(document);
}

ShowGayMales方法如下所示:

Widget showGayMales(DocumentSnapshot document) {      
   if (document['id'] == currentUserId || document['id'] == nopeId || ) {
     return Container();
   } else {
     return Container(
        child: Slidable(
          delegate: new SlidableScrollDelegate(),
          actionExtentRatio: 0.3,
        child: Card(
          child: Padding(
          padding:EdgeInsets.fromLTRB(20.0, 10.0, 25.0, 10.0),
          child: Row(
            children: <Widget>[
              Material(
                color: Colors.transparent,
                child: Icon(
                  FontAwesomeIcons.male,
                  color: textColor,
                ),
              ),
              new Flexible(
                child: Container(
                    child: new Column(
                      children: <Widget>[
                        new Container(
                          child: Text(
                      '${document['aboutMe']}',
                            style: TextStyle(color: textColor, fontSize: 30.0),
                        ),
                        alignment: Alignment.centerLeft,
                        margin: new EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 5.0),
                      ),
                      new Container(
                        child: Row(
                          children: <Widget>[
                            Text(
                            '-'+'${document['nickname'] ?? 'Not available'}',
                            style: TextStyle(color: textColor, fontSize: 15.0, fontWeight: FontWeight.bold),
                            ),
                            Text(
                              ','+' ${document['age'] ?? ''}'
                            )
                          ],
                        ),
                        alignment: Alignment.centerLeft,
                        margin: new EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
                      )
                    ],
                  ),
                  margin: EdgeInsets.only(left: 20.0),
                ),
              ),
            ],
          ),
          ),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
          ),
            actions: <Widget>[
             new IconSlideAction(
             caption: 'Not interested!',
             color: errorColor,
             icon: Icons.clear,
             onTap: () => notinterested('${document['id']}'),
             ),
            ],
            secondaryActions: <Widget>[
              new IconSlideAction(
              caption: "Interested!",
                color: primaryColor,
              icon: Icons.check,
              onTap: () => interested('${document['nickname']}', '${document['id']}', '${document['gender']}', '${document['aboutMe']}', '${document['age']}', '${document['preference']}'),
              ),
            ],
        ),
        margin: EdgeInsets.only(bottom: 10.0, left: 5.0, right: 5.0),
      );
   }
  }

回答(0)