sergey_s
sergey_s

Reputation: 143

How to sequentially send requests to the server?

I have a List, and I need to send each element of this List to the server sequentially. And if the answer is with an error, then do not continue sending, but display an error. I am trying to use

await Future.wait([]);

but I do not quite understand how to complete the send on failure, and how to iterate over the elements.

To iterate over elements through .forEach(element) {} The compiler throws an error: The argument type 'List' can't be assigned to the parameter type 'Iterable<Future>'

Here is what I am trying to do:

await Future.wait([
  state.revisionFill.forEach((element) {
    var successOrFail = await ContainerExpire.call(ContainerExpireParams(token: token, entity: element.entity));
  })
]);

I'm new and don't fully understand async/sync requests in flutter. Tell me which direction to study. How to complete the next send if it fails?

Upvotes: 0

Views: 208

Answers (1)

void void
void void

Reputation: 1224

This seems to be what you're looking for:

  for (var element in state.revisionFill) {
    var successOrFail = await ContainerExpire.call(ContainerExpireParams(token: token, entity: element.entity));
    if (successOrFail.hasFailed()) {
      //add code for some possible error handling
      break;
    }
  }

I am assuming that based on the value of successOrFail you can tell whether or not an error occured. Basically after each request you check for the status, and break on an error. You can add logic for displaying your error message.

Upvotes: 3

Related Questions