有没有办法使CircularProgressIndicator或Future持续最短的时间(发出HTTP请求时)? [英] Is there a way to make a CircularProgressIndicator or Future last a minimum amount of time (when making a HTTP request)?

查看:47
本文介绍了有没有办法使CircularProgressIndicator或Future持续最短的时间(发出HTTP请求时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于上下文,假设我有Flutter代码,当从API提取代码时,该代码显示 CircularProgressIndicator :

For context, suppose I have Flutter code which displays a CircularProgressIndicator while code is being fetched from an API:

Future<void> getItems() async {
  setState(ViewState.Busy);
  items = await _api.getItems();
  setState(ViewState.Idle);
}

为了使 CircularProgressIndicator 不会仅显示一秒钟的时间(我发现这会破坏相关动画的流程),因此我想使此指示器显示最短的时间.

So that the CircularProgressIndicator does not display for only a fraction of a second (which I find disrupts the flow of related animations), I want to make this indicator display for a minimum amount of time.

思考我可以使用Future API或计时器解决此问题,方法是设置将来HTTP请求可以使用的 minimum 时间.我该如何实现这一目标?

I think I can solve this problem using the Future API or a timer, by somehow setting the minimum amount of time a HTTP request future can take. How can I go about achieving this?

推荐答案

我经常使用此模式.将来等待接受一份期货清单.它并行执行它们,返回都将完成.我认为它看起来更干净.

I use this pattern frequently. Future-wait accept a list of Futures. It executes them all in parallel and returns will all have completed. I think it looks much cleaner.

https://api.dartlang.org/稳定/2.3.1/dart-async/Future/wait.html

  List<Future> futures = [];
  futures.add(Future.delayed(Duration(seconds: 1)));
  futures.add(_api.getItems());
  await Future.wait(futures);

这篇关于有没有办法使CircularProgressIndicator或Future持续最短的时间(发出HTTP请求时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆