首页 > 解决方案 > 有什么方法可以添加 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!');
              }
               }
            },
          )),
        );
      }
    }

标签: flutterdartmobile

解决方案


您可以使用变量来检查加载是否结束,例如首先设置 loading = true 然后在 dio set loading = false 加载 = true 时,显示 CircularProgressIndicator 并且在加载 false 时显示您想要的任何内容


推荐阅读