I want to use three functions at the same time asynchronously. I'm not sure that this is how I want it.
The only purpose of using inputPaymentData is that I want to use three different functions simultaneously, so I can get the result as fast as I can. I thought it would take more time if I don't use async and just use it synchronously way. Also, do I need to use await? I don't think I need to wait to get any data. Thanks.
void inputPaymentData(
{required String user,
required bool type,
required String merchantUid,
required String lessonUid,
required num amount,
required bool agree,
required String lesson,
required bool lessonCard,
required String? review,
required String coach}) async {
inputRecordData(
user: user,
type: type,
merchantUid: merchantUid,
lessonUid: lessonUid,
amount: amount);
inputReservationData(
user: user,
agree: agree,
lesson: lesson,
lessonCard: lessonCard,
review: review,
coach: coach);
increaseLessonNum(user);
}
void inputRecordData(
{required String user,
required bool type,
required String merchantUid,
required String lessonUid,
required num amount}) =>
paymentUseCase.inputRecordData(
user: user,
type: type,
merchantUid: merchantUid,
lessonUid: lessonUid,
amount: amount);
void inputReservationData(
{required String user,
required bool agree,
required String lesson,
required bool lessonCard,
required String? review,
required String coach}) =>
paymentUseCase.inputReservationData(
user: user,
agree: agree,
lesson: lesson,
lessonCard: lessonCard,
review: review,
coach: coach);
void increaseLessonNum(String user) =>
paymentUseCase.increaseLessonNum(user);
CodePudding user response:
I am using the format below for using async functions in the flutter [P.S I am a beginner in flutter too]
Future<return_type> functionName() async {
...
await callAPI(); //task that would take some time for execution like API calls
...
return <something of return_type>;
};
CodePudding user response:
Quick answer to your question. It isn't recommended to do like you did.
It is recommended to handle potential errors that might occur with your Future-calls. If you don't handle the failed Futures, they are uncaught and might crash your app.
This question has an excellent answer on the reason for why you shouldn't blindly do it like you've written, i.e. just fire away asynchronous methods.
Side note:
Your questions is ambigous. Or I misinterpreted you.. You say: "...so I can get the result as fast as I can." and at the same time you say: "I don't think I need to wait to get any data."
Those two statements contradict each other... Choose one, or clarify what you meant.
CodePudding user response:
Use the format below for async functions in flutter
getApiData() async {
response = await HttpServices.getCustomer();
}