Flutter 대표 카카오톡 개발자 톡을 운영중입니다.

https://open.kakao.com/o/gsshoXJ

 

Flutter 개발자 모임

#flutter #android #ios #안드로이드 #아이폰 #모바일 #선물요정소환

open.kakao.com


자바스크립트에서 많이 사용되는 Promise.all 을 다트에선 어떻게 구하는지 알아볼 예정입니다.

Promise.all 은 여러개의 Promise 를 모아서 한꺼번에 처리해주는 역할을 해준다. 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

 

Promise.all()

Promise.all() 메서드는 순회 가능한 객체에 주어진 모든 프로미스가 이행한 후, 혹은 프로미스가 주어지지 않았을 때 이행하는 Promise를 반환합니다. 주어진 프로미스 중 하나가 거부하는 경우, 첫 번째로 거절한 프로미스의 이유를 사용해 자신도 거부합니다.

developer.mozilla.org

그럼 다트에서는 어떻게 사용되는 지 살펴보겠다. 

void main() {
  final f1 = getValue(delay : 1, value: 1);
  final f2 = getValue(delay : 5, value: 2);
  final f3 = getValue(delay : 2, value: 3);
  final f4 = getValue(delay : 3, value: 4);
  
  Future.wait([f1,f2,f3,f4])
     .then((value) => print(value));
  
}

Future<int> getValue({int delay, int value}) => Future.delayed(Duration(seconds: delay), () => Future.value(value));

 문제는 저건 순서대로 진행될텐데 병렬로 할려면 어떻게 하는지 궁금 하신 분은(Isolate사용) 아래 링크에서 보시길 바랍니다.

https://buildflutter.com/flutter-threading-isolates-future-async-and-await/

 

Flutter Threading: Isolates, Future, Async And Await - Build Flutter

Flutter applications start with a single execution process to manage executing code. Inside this process you will find different ways that the process handles multiple pieces of code executing at the same time. Isolates When Dart starts, there will be one

buildflutter.com

'스터디 > Flutter' 카테고리의 다른 글

Flutter + Riverpod 카운터앱  (0) 2023.12.11
Flutter + Node + docker Sample Code  (0) 2019.06.14
Future of Flutter by Google IO  (0) 2019.05.22
커스텀 뷰 공부 1일차  (0) 2019.03.20
Flutter + Sqflite + Stetho 사용하기  (0) 2019.01.04

+ Recent posts