flutter - 集合中没有数据时显示占位符
问题描述
Text
当集合为空时,我试图显示一个小部件。Text('No Events :(');
但是,即使我的收藏为空,我也永远无法满足展示的条件。我认为这是飞镖语法错误?
Container(
height: 400,
// width: 500,
child: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('Events').where("bandId", isEqualTo:identifier ).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData)
return new Text('No Events :(');
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return new Text('Loading...');
default:
return new ListView(
children: snapshot.data.documents
.map((DocumentSnapshot document) {
return Dismissible(
key: new Key(document.documentID),
onDismissed: (direction){
Firestore.instance.runTransaction((transaction) async {
DocumentSnapshot snapshot=
await transaction.get(document.reference);
await transaction.delete(snapshot.reference);
});
Fluttertoast.showToast(msg: "Event Deleted");
},
child: CustomCard(
event: document['event'],
location: document['location'],
service: document['service'],
date: document['date'],
),
);
}).toList(),
);
}
},
)),
解决方案
您正在创建语法错误。您不能使用 new Text("Something"); new ListView( // ) 你必须指定一个支持儿童的小部件。您可以使用 Column 来显示多个小部件。
原因:您正在指定 If 和 Switch 大小写。我建议仅在 Switch case 或 If-Else 条件下覆盖您的逻辑。
Container(
height: 400,
// width: 500,
child: StreamBuilder(
stream: Firestore.instance.collection('Events').where("bandId", isEqualTo:identifier ).snapshots(),
builder: (BuildContext context,
AsyncSnapshot snapshot) {
return new Column(
children: [
if (!snapshot.hasData)
new Text('No Events :('),
switch (snapshot.connectionState) {
case ConnectionState.waiting:
new Text('Loading...');
default:
new Container(
childe: ListView(
children: snapshot.data.documents
.map((DocumentSnapshot document) {
return Dismissible(
key: new Key(document.documentID),
onDismissed: (direction){
Firestore.instance.runTransaction((transaction) async {
DocumentSnapshot snapshot=
await transaction.get(document.reference);
await transaction.delete(snapshot.reference);
});
Fluttertoast.showToast(msg: "Event Deleted");
},
child: CustomCard(
event: document['event'],
location: document['location'],
service: document['service'],
date: document['date'],
),
);
}).toList(),
),
),
}
]),
},
)
);
推荐阅读
- routing - Yii2 showScriptName 给我 404s
- c++ - QMetaObject::indexOfMethod() 寻找槽失败
- c# - 无法更改 IBM MQ XMS.NET 客户端重新连接超时
- import - 无法将字体导入 mjml
- javascript - 在页面加载期间无法更改表单子元素的边框颜色
- identityserver4 - UnauthorizedAccessException:对路径 'c:\windows\system32\inetsrv\tempkey.rsa' 的访问被拒绝。”
- c - C语言:在另一个文件中的main.c文件中使用枚举变量和类型定义的麻烦
- maven - 无需从存储库获取依赖项即可自动部署 EAR 项目
- css - Flexbox - 行内相同高度的列
- local-variables - Exasol 中的变量声明