WatsMyName
WatsMyName

Reputation: 4478

Flutter Dio package how to show onSendProgress data in widget

I am using Dio package for a http api request and it works pretty well. However i have a requirement that I want to show progress while sending data to server. I have following codes

CommonApiProvider.dart

static Future saveUserData(Map params) async {
    try {
      var body = json.encode(params);
      final response = await _dio!.post("${globals.apiUrl}SaveUserData/",
          data: body, 
          onSendProgress: (int sent, int total) {
              print("$sent kb of data sent of total $total kb");
      });
      if (response.statusCode == 200) {
        Map<String, String> d = Map<String, String>.from(response.data);
        return d;
      } else {
        return null;
      }
    } catch (exception) {
      return null;
    }
  }

And I am calling this from my GetX controller like this

Future saveUserData() async{
    Map params = {
      ....
    };

    var res = await CommonApiProvider.saveUserData(params);
    ....... 
}

The above code works and prints progress in debug console. however I want to show this information to user, in a Widget.

Can please someone tell me how to do this?

Upvotes: 0

Views: 646

Answers (1)

user18309290
user18309290

Reputation: 8300

One simple way is just to pass the same callback to saveUserData.

static Future saveUserData(Map params, ProgressCallback onSendProgress) async {
...
      final response = await _dio!.post("${globals.apiUrl}SaveUserData/",
          data: body, 
          onSendProgress: onSendProgress
      );
...
}

Upvotes: 1

Related Questions