kefah omran
kefah omran

Reputation: 1

is there any way to add CircularProgressIndicator while wating to get response form dio request

this is the auth file:

    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();
      }

In this main.dart file, I would like to use a CircularProgressIndicator widget

    @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!');
              }
               }
            },
          )),
        );
      }
    }

Upvotes: 0

Views: 405

Answers (2)

icp
icp

Reputation: 174

you can use a variable to check loading ended or not like first set loading = true and then after dio set loading = false when loading = true, show a CircularProgressIndicator and when loading false show whatever you want

Upvotes: 1

BLKKKBVSIK
BLKKKBVSIK

Reputation: 3548

You could use a FutureBuilder widget to call your async function, and during this time display a CircularProgressIndicator

FutureBuilder<bool>(
    future: myFuture,
    builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
         if (snapshot.data) {
            // Your stuff if you're logged
         } else {
               return CircularProgressIndicator(),
         }
   }
)

Upvotes: 0

Related Questions