[Dart] Futures, Async & Await
[Dart] Futures, Async & Await
[Ko]
- Future를 return하는 함수의 body 앞에는 Async를 붙이고, Future를 return하는 해당 code 앞에는 await를 붙임.
* Stateful Widget Lifecycle Methods
- return되는 Future를 받아서 사용하려면, Future를 return하는 method의 앞에 await를 붙이고, return 받는 Future를 variable에 저장해서 사용함.
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
- 위 코드에서 await를 붙이지 않으면 Future<Position> position = Geo…. 실제 완료된 position이 아닌 Position의 Future를 받게됨.
[En]
-Async is added to the body of the function that returns the Future, and await is added to the code that returns the Future.
* Stateful Widget Lifecycle Methods
- To receive and use the returned Future, add await in front of the method that returns the Future, and use the stored Future as a variable.
Position position = await Geolocator ()
.getCurrentPosition (desiredAccuracy: LocationAccuracy.high);
- If you don't add await in the code above, Future position = Geo… . Receive the Future of Position, not the actual completed position.