Home > Mobile >  Flutter Move to next line of code after getting answers from api
Flutter Move to next line of code after getting answers from api

Time:06-21

I have this function which calls 4 api.

  getDefects();
  getInstruction();
  getToDone();
  getToInvoice();

and after that it would update UI of my app.

  status.success();
  update(['all']);

i want this function to call all 4 api together and not wait for their answers but when the answer for all 4 api came back it moves to next part of code which updates UI

 getData() async {
 status.loading();

try {
  getDefects();
  getInstruction();
  getToDone();
  getToInvoice();
  
  status.success();
  update(['all']);

  } catch (e) {
  status.error(e.toString());
}
}

CodePudding user response:

updateState(int count) {
    if (count == 4) {
        status.success();
        update(['all']);
    }
}

getData() async {
    status.loading();
    int count = 0;

    try {
        getDefects().then(value => updateState(count  );
        getInstruction().then(value => updateState(count  );
        getToDone().then(value => updateState(count  );
        getToInvoice().then(value => updateState(count  );
    } catch (e) {
        status.error(e.toString());
    }
}
  • Related