Is it possible to invoke async function in flutter sync function without add the async keyword to parent function? I want to invoke the async function like this:
void getEventsForDay(DateTime day) {
var result = await TaskProvider.getTasksByRangeDate(monthStartMilliseconds, monthEndMilliseconds);
}
I could not add async keywords in the function getEventsForDay because the outer component did not allow. But I still need to wait the getTasksByRangeDate return to load some initial data. what should I do to invoke an async function without add async keywords in parent function? BTW, this is the full code:
List<TodoTask> getEventsForDay(DateTime day) {
int monthStartMilliseconds = DateTimeUtils.startOfMonthMilliseconds(DateTime.now());
int monthEndMilliseconds = DateTimeUtils.endOfMonthMilliseconds(DateTime.now());
var result = await TaskProvider.getTasksByRangeDate(monthStartMilliseconds, monthEndMilliseconds);
tasks.addAll(result);
buildHashMap(result);
final DateFormat formatter = DateFormat('yyyy-MM-dd');
String dateString = formatter.format(day);
List<TodoTask> dayTodoTask = taskMap[dateString] ?? [];
return dayTodoTask;
}
I have already tried using then like this:
List<TodoTask> getEventsForDay(DateTime day) {
int monthStartMilliseconds = DateTimeUtils.startOfMonthMilliseconds(DateTime.now());
int monthEndMilliseconds = DateTimeUtils.endOfMonthMilliseconds(DateTime.now());
TaskProvider.getTasksByRangeDate(monthStartMilliseconds, monthEndMilliseconds).then((value) => {getTasks(value, day)});
}
List<TodoTask> getTasks(List<TodoTask> tasks, DateTime day) {
tasks.addAll(tasks);
buildHashMap(tasks);
final DateFormat formatter = DateFormat('yyyy-MM-dd');
String dateString = formatter.format(day);
List<TodoTask> dayTodoTask = taskMap[dateString] ?? [];
return dayTodoTask;
}
seems could not return the task list to outer layer funciton. I have tried like this:
List<TodoTask> getEventsForDay(DateTime day) {
var taskResult;
int monthStartMilliseconds = DateTimeUtils.startOfMonthMilliseconds(DateTime.now());
int monthEndMilliseconds = DateTimeUtils.endOfMonthMilliseconds(DateTime.now());
TaskProvider.getTasksByRangeDate(monthStartMilliseconds, monthEndMilliseconds)
.then((value) => {() => taskResult = getTasks(value, day)});
return taskResult ?? [];
}
List<TodoTask> getTasks(List<TodoTask> tasks, DateTime day) {
tasks.addAll(tasks);
buildHashMap(tasks);
final DateFormat formatter = DateFormat('yyyy-MM-dd');
String dateString = formatter.format(day);
List<TodoTask> dayTodoTask = taskMap[dateString] ?? [];
return dayTodoTask;
}
but the taskResult result always be Null.