如何从Flutter的listView中删除空项目

问题描述 投票:0回答:1

我正在尝试从Firebase数据库检索数据,在这里我可以将它们显示为列表项,用户可以在其中选择一项,

listview检索的内容与我在firebase中的集合中的内容一样多,并且根据过滤器,也许只有1或2项仅对显示有效,ListView占用的空间与我在数据库中的项目一样多。

return StreamBuilder(
  stream: _firestore.collection('trips').snapshots(),
  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
    if (!snapshot.hasData) return Text('Loading...');

    return ListView(
      children: snapshot.data.documents.map((document) {
        return Padding(
          padding: const EdgeInsets.all(15.0),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Text(
                  document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination)  ?'From : ' + document['currentLocation']:'x',
                  style: TextStyle(
                      fontWeight: FontWeight.w900, fontSize: 25.0),
                ),
                SizedBox(
                  height: 5.0,
                ),
                Text(

                  document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination)  ?     'To : ' + document['destination']:'x',
                  style: TextStyle(
                      fontWeight: FontWeight.w900, fontSize: 20.0),
                ),
                SizedBox(
                  height: 25.0,
                ),
                Text( document['time'].contains(ampm)&&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ? 'Trip Time     : ' + document['time']:'x'),
                SizedBox(
                  height: 15.0,
                ),
                Text(document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ? ' Trip Details : ' + document['details']:'x'),
                SizedBox(
                  height: 15.0,
                ),
                Text(document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ? 'Driver Email  : ' + document['driverEmail']: 'x'),
                SizedBox(
                  height: 15.0,
                ),
                Text(document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ? 'Trip Price      : ' + document['price'] + 'JD' :'x'),
                SizedBox(
                  height: 15.0,
                ),
                Text(document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ? 'Avilable Seates : ' + document['avilableSeates'].toString()  :'x'),

                SizedBox(
                  height: 25.0,
                ),
                RoundedButton(
                  onPressed: document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ?() {
                    print(userInputTime);

                    showDialog(
                        context: context,
                        builder: (BuildContext context) {
                      // return object of type Dialog
                      return AlertDialog(
                        // todo some style on text Trip info
                        // todo check if the user had already selected the same trip. or he have a trip on the way
                        title:   Text('Trip Selected'),
                        content:   Text(document['currentLocation'] + ' to ' +  document['destination'] +' At ' + document['time'] +' ?' ),
                        actions: <Widget>[
                          // usually buttons at the bottom of the dialog
                            FlatButton(
                            child:   Text('Yes'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                          FlatButton(
                            child:   Text('No'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                        ],
                      );
                    });



                  } : null,
                  title:document['time'].contains(ampm) &&checkMatchLocationsAndDestinations(document['currentLocation'], document['destination'], uInputCurrentLocation, uInputDestination) ? 'Select ' : 'No Trips Found !',
                  color: Colors.blueAccent,

//待办时间旅行),],),),);})。toList(),//'Here I NEED TO REMOVE NULL TEXT WIDGTES ITEMS FROM THE LIST');

flutter flutter-layout flutter-test
1个回答
1
投票
您可以执行以下操作:

// ... inside your .map() snapshot.data.documents.map((document) { if(document == null) return Wrap(); // rest of map logic }

© www.soinside.com 2019 - 2024. All rights reserved.