[英]How to make progress bar for receiving response from API?
while receiving data from API to flutter app it takes time to receive.在从 API 接收数据到 Flutter 应用程序时,它需要时间来接收。 How to make a progress bar to load until the JSON data is received to the flutter app and move to next Screen of the app.如何使进度条加载,直到 JSON 数据接收到 Flutter 应用程序并移动到应用程序的下一个屏幕。
I'm not using ListView.Builder.我没有使用 ListView.Builder。 Instead I use it for texts, dropdown and other things also.相反,我也将它用于文本、下拉菜单和其他内容。
You could use a FutureBuilder widget, as shown in the cookbook for retrieving and displaying data :您可以使用 FutureBuilder 小部件,如用于检索和显示数据的食谱所示:
FutureBuilder<Album>(
future: futureAlbum,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.title);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
);
This widget allows you to build your layout with information about the data your are loading and the status of the load itself.此小部件允许您使用有关正在加载的数据和加载本身的状态的信息来构建布局。
For the future:
parameter, give him your http.get
request.对于future:
参数,给他你的http.get
请求。
In the builder:
param, give it an anonymous function that builds the content you want to display.在builder:
param 中,给它一个匿名函数来构建你想要显示的内容。 In said closure, you can rely on the snapshot
to know wether the data has finished downloading ( snapshot.hasData
) or if there was an error downloading ( snapshot.hasError
).在上述闭包中,您可以依靠snapshot
来了解数据是否已完成下载( snapshot.hasData
)或下载是否有错误( snapshot.hasError
)。 To display the progress bar you asked for, you can infer the status : if the snapshot has no data nor any error, therefore it is loading and you return you progress bar to the user.要显示您要求的进度条,您可以推断状态:如果快照没有数据或任何错误,则它正在加载,您将进度条返回给用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.