Widget _drawTopStories() {
return Container(
color: Colors.grey.shade50,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.only(left: 16, top: 10),
child: _drawTitle("TOP STORIES"),
),
Padding(
padding: EdgeInsets.all(8),
child: Card(
child: FutureBuilder(
future: postapi.fetchWhatNew(),
builder: (context, AsyncSnapshot snapShot) {
Post post1 = snapShot.data[0];
Post post2 = snapShot.data[1];
Post post3 = snapShot.data[2];
return Column(
children: <Widget>[
_drawSingleRow(post1),
_drawDiver(),
_drawSingleRow(post2),
_drawDiver(),
_drawSingleRow(post3),
],
);
},
),
),
),
],
),
);
}
答案 0 :(得分:1)
除非确定快照中有数据,否则不应使用该快照。
if (snapShot.hasData) {
Post post1 = snapShot.data[0];
Post post2 = snapShot.data[1];
Post post3 = snapShot.data[2];
return Column(
children: <Widget>[
_drawSingleRow(post1),
_drawDiver(),
_drawSingleRow(post2),
_drawDiver(),
_drawSingleRow(post3),
],
);
}
return CircularProgressIndicator();
答案 1 :(得分:0)
这可能有两个原因:
第一个原因:
由于Future Builder是异步生成器,因此在显示任何小部件之前,您需要先检查ConnectionState
。
if(snapshot.ConnectionState == ConnectionState.waiting) return CricularProgressIndicator();
第二个原因:
实际的快照可能为null,这意味着您没有从fetchWhatNew
接收任何数据。因此,您需要先检查if(snapshot.hasData)
,它会告诉您是否正在接收数据或为空。