[FLUTTER] 다트 주요 라이브러리 둘러보기 - 5 / A Tour of the Dart Libraries
dart·@wonsama·
0.000 HBD[FLUTTER] 다트 주요 라이브러리 둘러보기 - 5 / A Tour of the Dart Libraries
 > 출처 : https://www.dartlang.org/guides/libraries/library-tour * 다트 라이브러리의 주요 기능의 사용 방법 및 예시를 볼 예정 입니다. * TL;DR : 최대한 내용을 요약하여 표현했습니다. * 좀 더 자세하고 상세한 내용을 보고 싶으시면 [위 원문 링크](https://www.dartlang.org/guides/libraries/library-tour)를 참조 바랍니다. <sub>[플러터란 ?](https://namu.wiki/w/Flutter(%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC)) 구글에서 개발한 크로스 플랫폼 앱 개발 프레임워크며, 언어는 구글이 개발한 Dart 를 사용합니다. 안드로이드, iOS, Web, Desktop 을 지원하며 구글의 차기 OS Fuchsia의 메인개발환경이 된다고 하네요 :) </sub> # 이전 글 * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 4 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-4-a-tour-of-the-dart-libraries) * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 3 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-3-a-tour-of-the-dart-libraries) * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 2 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-2-a-tour-of-the-dart-libraries) * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 1 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-1-a-tour-of-the-dart-libraries) # 9. Stream > 스트림(stream : 흐름) 객체는 데이터 시퀀스(sequence : 연속적흐름) 를 나타냅니다. 예를 들어 버튼 클릭과 같은 HTML 이벤트는 스트림을 사용하여 전달됩니다. 파일을 스트림으로 읽을 수도 있습니다. ## 9.1. 비동기 for 반복문 사용 > Stream의 listen() 메서드를 사용하여 파일 목록의 정보를 구독(subscribe) 하고 각 파일이나 디렉토리를 검색하는 함수 리터럴을 전달합니다. * 폴더 내부를 반복적(recursive)으로 돌면서 파일 정보를 탐색하는 예제 ``` void main(List<String> arguments) { // ... FileSystemEntity.isDirectory(searchPath).then((isDir) { if (isDir) { final startingDir = Directory(searchPath); startingDir .list( recursive: argResults[recursive], followLinks: argResults[followLinks]) .listen((entity) { if (entity is File) { searchFile(entity, searchTerms); } }); } else { searchFile(File(searchPath), searchTerms); } }); } ``` * await for 를 사용하여 위와 동일하게 구현할 수 있습니다. ``` Future main(List<String> arguments) async { // ... if (await FileSystemEntity.isDirectory(searchPath)) { final startingDir = Directory(searchPath); await for (var entity in startingDir.list( recursive: argResults[recursive], followLinks: argResults[followLinks])) { if (entity is File) { searchFile(entity, searchTerms); } } } else { searchFile(File(searchPath), searchTerms); } } ``` [중요] : `await for` 를 활용하여 기다리기 전에 코드가 명확하고 스트림 결과 전체를 기다리고 싶는지를 확인해야 됩니다. 일반적으로 dom 개체의 이벤트 정보는 무한으로 들어 올 수 있기 때문입니다. ## 9.2. 스트리밍 데이터 수신대기(listening) > 데이터가 도착하는대로 각각의 값을 가져 오려면, listen() 메서드를 사용하여 스트림을 기다리거나(wating) 구독(subscribe)을 사용하십시오. ``` // 버튼의 ID를 찾은 후 이벤트 등록 querySelector('#submitInfo').onClick.listen((e) { // 버튼이 클릭되면 아래 코드가 수행됨 submitData(); }); ``` ## 9.3. 스트리밍 데이터 변환 > 스트림(stream) 데이터를 사용하려면 먼저 스트림의 데이터 형식으로 변경해야합니다. transform() 메서드를 사용하여 다른 유형의 데이터가 있는 스트림을 생성합니다. ``` var lines = inputStream .transform(utf8.decoder) // utf8 변환 .transform(LineSplitter()); // line(줄) 단위 변환처리 ``` ## 9.4. 오류 및 완료처리 * `try~cath` 를 활용하여 오류 처리 예시 ``` Future readFileAwaitFor() async { var config = File('config.txt'); Stream<List<int>> inputStream = config.openRead(); var lines = inputStream .transform(utf8.decoder) .transform(LineSplitter()); try { await for (var line in lines) { print('Got ${line.length} characters from stream'); } print('file is now closed'); } catch (e) { print(e); } } ``` * `onError` 를 활용하여 오류 처리 예시 ``` var config = File('config.txt'); Stream<List<int>> inputStream = config.openRead(); inputStream .transform(utf8.decoder) .transform(LineSplitter()) .listen((String line) { print('Got ${line.length} characters from stream'); }, onDone: () { print('file is now closed'); }, onError: (e) { print(e); }); ``` ## 9.5. 기타 관련 글 * [비동기 프로그래밍 : Future Asynchronous Programming: Futures](https://www.dartlang.org/tutorials/language/futures) * [Future 와 오류처리 - Futures and Error Handling](https://www.dartlang.org/guides/libraries/futures-error-handling) * [이벤트 반복 - The Event Loop and Dart](https://webdev.dartlang.org/articles/performance/event-loop) * [비동기 프로그래밍 : Stream - Asynchronous Programming: Streams](https://www.dartlang.org/tutorials/language/streams) * [스트림 만들기 - Creating Streams in Dart](https://www.dartlang.org/articles/libraries/creating-streams) # 맺음말 > 이번 시간에는 Stream 에 대해 알아봤습니다. 일반적으로 파일 처리에 많이 사용되며 연속적으로 들어오는 데이터(Stream)를 나눠서(chunking) 처리할 때 유용하게 사용 될 수 있습니다. * 이전 시간에 배운 A Tour of the Dart Language [ 1 ](https://steemit.com/dart/@wonsama/flutter-dart-1-a-tour-of-the-dart-language) [ 2 ](https://steemit.com/dart/@wonsama/flutter-dart-2-a-tour-of-the-dart-language) [ 3 ](https://steemit.com/dart/@wonsama/flutter-dart-3-a-tour-of-the-dart-language) [ 4 ](https://steemit.com/dart/@wonsama/flutter-dart-4-a-tour-of-the-dart-language) 는 여유 되시면 반복 학습 부탁 드립니다. 위 내용이 학습된 상태에서 본 내용을 습득해야 이해가 빠른편인지라 ... :) ## 읽기 힘든 글 읽어주셔서 (또는 눈팅 이라도) 감사합니다 # 관련 글 링크 > @flutters : 제가 작성한 글 중 fluter 관련 글만 모아서 리스팀 처리 ### 관련글(영문) * [Dart Language Specification](https://www.dartlang.org/guides/language/spec) * [Effective Dart](https://www.dartlang.org/guides/language/effective-dart) ### 연재글 * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 4 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-4-a-tour-of-the-dart-libraries) * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 3 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-3-a-tour-of-the-dart-libraries) * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 2 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-2-a-tour-of-the-dart-libraries) * [[FLUTTER] 다트 주요 라이브러리 둘러보기 - 1 / A Tour of the Dart Libraries](https://steemit.com/dart/@wonsama/flutter-1-a-tour-of-the-dart-libraries) * [[FLUTTER] DART 언어 기초과정 - 4 / A Tour of the Dart Language](https://steemit.com/dart/@wonsama/flutter-dart-4-a-tour-of-the-dart-language) * [[FLUTTER] DART 언어 기초과정 - 3 / A Tour of the Dart Language](https://steemit.com/dart/@wonsama/flutter-dart-3-a-tour-of-the-dart-language) * [[FLUTTER] DART 언어 기초과정 - 2 / A Tour of the Dart Language](https://steemit.com/dart/@wonsama/flutter-dart-2-a-tour-of-the-dart-language) * [[FLUTTER] DART 언어 기초과정 - 1 / A Tour of the Dart Language](https://steemit.com/dart/@wonsama/flutter-dart-1-a-tour-of-the-dart-language) ### 설치/설정 * [[flutter] #001. 설치하기 ( in Windows )](https://steemit.com/kr/@wonsama/flutter-1-1542807335745) --- <center><a href="https://www.gopax.co.kr"></a></center>
👍 newbijohn, steeming-hot, accelerator, effofex, steemchoose, noreference, merlin7, ctime, songbj, donekim, ukk, kakakk, seapy, cyan2017, steemitag, xxnoaxx, jayplayco, mishana, culam92, sisilafamille, krfeed, mooyeobpark, captainstar, yangpankil27, samuel-vasquez, guest123, joceo00, fur2002ks, codingart, jinuking, jjangjjanggirl, steem.series, hersnz, stylegold, kimegggg, anpigon, ryanhkr, bbooaae, lucky2,