flutter - 如何从 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 文本小部件项' );
解决方案
你可以这样做:
// ... inside your .map()
snapshot.data.documents.map((document) {
if(document == null) return Wrap();
// rest of map logic
}
推荐阅读
- primefaces - 如何在 ap:dataTable 中使用“sortBy”和 HashSet 集合
- hololens-emulator - 将文件复制到 HoloLens 模拟器
- flutter - 将 ListView 嵌套到 DropdownMenu
- python - sklearn train_test_split - ValueError:找到样本数量不一致的输入变量
- vue.js - 如何重置组件的状态值?
- javascript - discord.js - 从所有成员中删除特定角色
- gunicorn - FastAPI gunicon uvicorn access_log 格式定制
- php - property_exists() 检查类方法中是否存在静态属性
- maven - Maven插件未执行
- typescript - 使用一组承诺调用 Promise.all 时,Typescript 会引发错误