flutter - 有什么方法可以添加 CircularProgressIndicator 以获取响应表单 dio 请求
问题描述
这是授权文件:
Future attempt(String token) async {
try {
Dio.Response response = await dio().get(
'/user',
options: Dio.Options(
headers: {'Authorization': 'Bearer $token'},
followRedirects: false,
validateStatus: (status) {
return status! < 500;
}),
);
'get user info '
_user = User.fromJson(json.decode(response.toString()));
_authenticated = true;
// return response;
} catch (e) {
_authenticated = false;
}
notifyListeners();
}
在这个 main.dart 文件中,我想使用一个CircularProgressIndicator
小部件
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
drawer: NavDrawer(),
body: Center(child: Consumer<Auth>(
builder: (context, auth, child) {
if (auth.authenticated) {
return Text('You are logged in!');
} else {
return Text('You are not logged in!');
}
}
},
)),
);
}
}
解决方案
您可以使用变量来检查加载是否结束,例如首先设置 loading = true 然后在 dio set loading = false 加载 = true 时,显示 CircularProgressIndicator 并且在加载 false 时显示您想要的任何内容
推荐阅读
- c# - 首先使用代码上传 mvc5 应用程序文件而不在实时站点中发布文件
- kubernetes - 如何在 kubernetes 中使用 consul 作为默认 dns 或服务发现
- android - 运行时 IllegalStateException 预期开始数组,但为字符串
- javascript - 生成 10 个唯一的随机整数
- html - 3 列固定标题,可扩展高度以适应内容
- angular - 导航后角度数据不更新
- reactjs - 传入 React.Component 类型的 prop 时出现 TS 错误
- c# - EF Core 2.1 GROUP BY 并选择每个组中的第一项
- python - 当我想使用 python pandas 仅过滤列中的部分值时,如何在 Pandas 中使用过滤器?
- android - 通过 adb 阻止单个 apk 通知