如果服务器返回的数据是空的,那个列表是null值,那_items.length就会报错,所以要添加判断,代码如下
关键代码在这里
body: (_items != null)
? Container(
//设置界面背景
decoration: BoxDecoration(
color: Color(0xFF222222),
),
child: ListView.builder(
itemCount: _items.length,
itemBuilder: itemView,
))
: Container(
//设置界面背景
width: width,
height: height,
decoration: BoxDecoration(
color: Color(0xFF222222),
),
child: Column(
children: [
Container(
margin: EdgeInsets.only(top: 30),
child: Text("暫無消息",style: TextStyle(color: Color(0xFF666666),fontSize: 14),),
)
],
),
),
相对完整代码如下
@override
Widget build(BuildContext context) {
return Material(
child: Scaffold(
appBar: AppBar(
//leading设置状态栏左边的图标
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Container(
margin: const EdgeInsets.only(left: 10.0),
width: 20,
child: Image.asset(
'assets/base_widgets/icon_register_back.png',
fit: BoxFit.fitWidth,
),
),
),
actions: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: () {
Navigator.push(context,
DialogRouter(PublishMomentBackDialog(true, newest)))
.then((value) => getCommentList());
},
child: Container(
margin: EdgeInsets.only(right: 10),
child: Text(
"清空",
style: TextStyle(color: Color(0xFF999999), fontSize: 14),
),
),
),
],
),
],
title: Text('動態訊息'),
backgroundColor: Color(0xFF222222),
//文字居中
centerTitle: true,
),
body: (_items != null)
? Container(
//设置界面背景
decoration: BoxDecoration(
color: Color(0xFF222222),
),
child: ListView.builder(
itemCount: _items.length,
itemBuilder: itemView,
))
: Container(
//设置界面背景
width: width,
height: height,
decoration: BoxDecoration(
color: Color(0xFF222222),
),
child: Column(
children: [
Container(
margin: EdgeInsets.only(top: 30),
child: Text("暫無消息",style: TextStyle(color: Color(0xFF666666),fontSize: 14),),
)
],
),
),
));
}