如何从 Flutter 中的 listView 中删除空项目
how to remove null items from listView in Flutter
我正在尝试从 firebase 数据库中检索数据,我可以在其中将它们显示为列表项,用户可以在其中 select 一项,
listview 检索的项目与我在 firebase 中的集合中的项目一样多,根据过滤器,可能只有 1 或 2 个项目只能显示,
ListView 占用 space 与我在数据库中拥有的项目一样多。
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关闭时间旅行
),
],
),
),
);
}).toList(), //' Here I NEED TO REMOVE NULL TEXT WIDGTES ITEMS FROM THE LIST'
);
你可以这样做:
// ... inside your .map()
snapshot.data.documents.map((document) {
if(document == null) return Wrap();
// rest of map logic
}
我正在尝试从 firebase 数据库中检索数据,我可以在其中将它们显示为列表项,用户可以在其中 select 一项,
listview 检索的项目与我在 firebase 中的集合中的项目一样多,根据过滤器,可能只有 1 或 2 个项目只能显示, ListView 占用 space 与我在数据库中拥有的项目一样多。
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关闭时间旅行 ), ], ), ), ); }).toList(), //' Here I NEED TO REMOVE NULL TEXT WIDGTES ITEMS FROM THE LIST' );
你可以这样做:
// ... inside your .map()
snapshot.data.documents.map((document) {
if(document == null) return Wrap();
// rest of map logic
}