[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.
'플러터(Flutter) > 다트(Dart)' 카테고리의 다른 글
[Dart] list combine method (0) | 2020.04.17 |
---|---|
[Dart] Exception Handling & Null Aware Operators (0) | 2020.03.19 |
[Dart] Maps (0) | 2020.03.19 |
[Dart] Functions as First Order Objects (0) | 2020.03.19 |
[Dart] Enums (0) | 2020.03.19 |