首页 > 解决方案 > 如何从 Flutter 中的 listView 中删除 null 项

问题描述

我正在尝试从 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,

// todo close time trips ), ], ), ), ); }).toList(), //'这里我需要从列表中删除 NULL 文本小部件项' );

标签: flutterflutter-layoutflutter-test

解决方案


你可以这样做:

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

推荐阅读