flutter - 颤振条件渲染。不工作?
问题描述
颤振条件渲染。不工作?。我确实尝试了很多方法,但我找不到任何解决方案。我想在data[index].status
不为空时渲染一个小部件。我的代码对我不起作用。
现在我遇到了另一个问题元素类型“Set”不能分配给列表类型“Widget”。
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFecf0f1),
appBar: AppBar(
actions: <Widget>[],
),
body: Column(
children: <Widget>[
Expanded(
child: ListView.builder(
padding: EdgeInsets.all(10),
itemCount: data.length,
itemBuilder: (BuildContext context, int index) {
return Card(
margin: EdgeInsets.all(10),
child: Padding(
padding: EdgeInsets.all(5),
child: Container(
child: Column(
children: <Widget>[
Container(
child: Row(
children: <Widget>[
Container(
child: Text(data[index].airline,
style: TextStyle(
fontFamily: '',
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.w300,
)),
),
if (data[index].status == null)
{
Container(
child: Text('somthing'),
)
}
else
{
Container(
decoration: BoxDecoration(
color: Colors.red[700],
borderRadius:
BorderRadius.circular(20)),
padding: EdgeInsets.symmetric(
vertical: 6, horizontal: 8),
child: Text(
data[index].status,
style: TextStyle(
fontSize: 15,
color: Colors.white,
fontWeight: FontWeight.normal,
fontFamily: 'Raleway'),
),
),
}
],
),
),
],
),
),
),
);
},
),
),
],
),
);
}
}
解决方案
集合 ifif
将/子句之后的花括号解释else
为不是代码块,而是set
文字:
此代码编译正常:
class GoodWidget extends StatelessWidget {
final bye = false;
@override
Widget build(BuildContext context) {
return Column(children: [
if (bye) Text('Goodbye') else Text('Hello'),
Text('World!')
]);
}
}
这段代码会产生错误
The element type 'Set<Container>' can't be assigned to the list type 'Widget'
class BadWidget extends StatelessWidget {
final bye = false;
@override
Widget build(BuildContext context) {
return Column(children: [
if (bye) {Text('Goodbye')} else {Text('Hello')},
Text('World!')
]);
}
}
在这种情况下,不允许使用大括号。
推荐阅读
- javascript - 应用内浏览器中缺少 Facebook 登录按钮
- php - 每个循环的 Laravel 都会产生额外的列表
- django - 在 django 中查询?
- delphi - FastMM:如何为所有 unicode 或 ansi 字符串注册预期内存泄漏?
- php - PHP CLI 运行外部程序
- ios - 如何为泛型类及其子类提供协议方法的默认实现?
- html - 如何在两个不同的页面之间链接
- ruby-on-rails - Ruby on Rails - 加载活动记录的记录范围与收集方法
- python - 使用 Cloud Composer 和 Celery Executor 在安全网络中的远程工作人员上运行作业
- firebase - Firebase 动态链接社交信息