解决Flutter 服务器返回空值的时候,列表会报错

18 min read
  @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),),
                  )
                ],
              ),
            ),
    ));
  }