我的 snapshot.data 为空。当我打印响应时,它显示检索到的数据。但 snapshot.data 仍然为空。
Future _getUsers() async {
var data = await http.post("http://10.0.2.2/Flutter/abreport.php", body: {
{
"date": mydt,
});
var jsonData = json.decode(data.body); //edited
print(jsonData); // the data is printing here
return jsonData;
}
}
FutureBuilder(
future: _getUsers(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
debugPrint(snapshot.data);
if (snapshot.data == null) {
return Container(
child: Center(
child:Text("no data"),
)
);
} else {
//some code
}
)
Run Code Online (Sandbox Code Playgroud)
小智 9
您应该使用文档中给出的格式。FutureBuilder
您没有检查未来的状态,因此FutureBuilder
首次构建时,它将显示"no data"
。您还没有实现您的else
分支,因此当您拥有数据时,您的构建可能不会刷新。试试这个代码:
FutureBuilder(
future: _getUsers(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data == null) {
return Text('no data');
} else {
return Text('data present');
}
} else if (snapshot.connectionState == ConnectionState.error) {
return Text('Error'); // error
} else {
return CircularProgressIndicator(); // loading
}
}
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19204 次 |
最近记录: |