我对fluter还是很陌生,我四处张望,找不到解决我错误的方法。我想从api调用返回结果打印,但是我无法这样做。我最终得到“未来实例”,而不是打印列表
我不断得到的错误
“ Future
”的实例
我的代码
class _SuperVisorSheetState extends State<SuperVisorSheet> {
Future <List> clusters;
Future<List> getClusters() async{
var response = await Dio().get('http://192.168.100.3:8080/api/v1/clustertable');
return response.data;
}
@override
void initState() {
// TODO: implement initState
clusters = getClusters();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Color(0xff392850),
title: Text("Supervisor "),
),
body: Container(
padding: EdgeInsets.all(10),
child: FutureBuilder<List>(
future: clusters,
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if(snapshot.hasData){
print(clusters);
return Text("Hello");
}
return null;
},
)
答案 0 :(得分:1)
首先,您不应该从null
返回FutureBuilder
。要访问数据,您应该使用snapshot.data
。像这样:
FutureBuilder<List>(
future: clusters,
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if (snapshot.hasData) {
print("clusters: ${snapshot.data.join(", ")}");
return Text("Hello");
}
return SizedBox();
},
)