Develop/Flutter

[Flutter] Error: The await expression can only be used in an async function. (Solved)

마크투비 2022. 1. 14. 15:10

Error: The await expression can only be used in an async function. (Solved)


💢 Error 메시지

The await expression can only be used in an async function. Try making the function body with either 'async' or 'async*'. A value of type 'future<diarylist>' can't be assigned to a variable of type 'list<diary>'. 

try changing the type of the variable, or casting the right-hand type to 'list<diary>'.

 

 

📍 Trial and Error1

GetFromServer().getDiaryList() 함수가 Future<DiaryList>를 리턴해서 items에 할당할 수 없다는 에러

→ 함수 앞에 await 키워드를 써줌

 

 

 

📍 Trial and Error2

setState 함수 안이 아닌 밖에서 함수를 호출해준다. 여전히 에러 메시지가 뜬다.

 

 

 

✅ Solved!!

setState 함수 안에서 함수를 호출하는데, setState를 async로 선언해주면 된다. (그리고 List<Diary> 형이었던 변수 items가 아닌 DiaryList 형 변수 diarylist에 할당시켰다.)

 

'Develop > Flutter' 카테고리의 다른 글

[Flutter] editMode 추가하기  (0) 2022.01.10
[Flutter] 플러터 벼락치기⚡ (일기 앱 만들기)  (0) 2021.12.29