在{ (builde:(context,snapshot)之前,我的代码中有这个错误
buildSearchresult() {
return FutureBuilder(
future: searchresultfuture,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgress();
}
});
}发布于 2022-04-21 12:55:17
builder必须始终返回一个小部件。但是,在您的代码中,如果条件!snapshot.hasData不满足,则builder返回null。因此,您应该在此条件之外返回一个小部件:
buildSearchresult() {
return FutureBuilder(
future: searchresultfuture,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgress();
}
return Container(); // The widget containing your data
});
}发布于 2022-04-21 12:55:57
可能是duplicate。
只有在没有数据时才返回小部件。您需要在每种可能的情况下返回小部件,例如
您还可以通过返回默认的小部件别人状态来简化。
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
/// return your widget while loadling
} else if (snapshot.hasError) {
/// return your widget based on error
} else if (snapshot.hasData) {
/// return your widget while have data
} else if (!snapshot.hasData) {
/// return your widget while there is no data
} else {
/// return widget
}
},发布于 2022-04-21 12:57:04
你把return忘在FutureBuilder()上了。您拥有的return仅用于您的if语句,而不是FutureBuilder()。
使用:
buildSearchresult() {
return FutureBuilder(
future: searchresultfuture,
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgress();
}
return ... // add this to return something.
});
}有关更多信息,请参见类似的问题here。
https://stackoverflow.com/questions/71954753
复制相似问题