프로그래밍/다트(dart)
-
스트림 통신하기프로그래밍/다트(dart) 2021. 12. 5. 15:48
순서를 보장받으면서 데이터를 주고받을때 스트림을 사용한다. 먼저 들어간 데이터가 먼저 나오는 자료구조 중 큐 형태라고 볼 수 있다 (FIFO) countStream 함수는 비동기로 계속 숫자를 차례대로 1부터 넘겨주고 있으며 sumStream 함수는 해당값을 계속 받으며 총 합을 계산하고 값을 다 더한 뒤 총합을 반환한다. import 'dart:async'; Future sumStream(Stream stream) async{ var sum = 0; await for (var value in stream){ print('sumStream : $value'); sum += value; } return sum; } Stream countStream(int to) async* { for (int i = 1; i
-
json 데이터 처리프로그래밍/다트(dart) 2021. 12. 5. 15:25
import 'dart:convert'; void main(){ var jsonString = ''' [ {"name": "철수"}, {"name": "영희"} ] '''; var people = jsonDecode(jsonString); var earlyPerson = people[0]; print(people is List); print(earlyPerson is Map); print("early Pserson name is " + earlyPerson['name']); } 결과 1. true 2. true 3. early Pserson name is 철수 예시 데이터를 jsonDecode를 하게되면 List 자료형으로 반환된다.
-
비동기 처리프로그래밍/다트(dart) 2021. 12. 5. 15:15
비동기 처리 void main(){ processOne(); processTwo(); processThree(); processFour(); } void processOne(){ print('process one action'); } # 1초 뒤에 실행시킴, 아래의 프로세스는 실행됨 void processTwo(){ Future.delayed(Duration(seconds:1),(){ print('processTwo Future delay 1 seconds'); }); print('porcess two action'); } # 2초 뒤 실행시킴, 아래의 프로세스는 비동기 처리 이후에 실행됨 void processThree() async{ await F..