I use a button to signout from app, need to update data before signout.
but before update data the user signout!! and this get error without updating data.
so how to fix this problem?
here my code
CodePudding user response:
Because of dart event loop, first of all, completes a sync code, in your case it is:
Navigator.of(context).pop()
Then, it completes a future in completed order, which means, faster response - faster completion. To make your async code (futures) complete in order like sync, you should pass await keyword before futures, for example, it works like this:
await auth.UpdateData('state', 'offline'); // - completes first
await _signout(context); // - second
Navigator.of(context).pop; // - third
If you'll not use await keyword, order will look like this:
auth.UpdateData('state', 'offline'); // - query send and wait for response
_signout(context); // - query send and wait for response (logout is usually faster than setting data, that means your user signout first, then - user update data)
Navigator.of(context).pop; // - completes first
CodePudding user response:
solution 1:
Future.delayed(const Duration(seconds: 2), () {
//what you want after 2 second
});
solution 2: put await before auth.updatData