본문 바로가기

Dart

(14)
[Dart] Class - getter, setter void main() { House house1 = new House(4,2); house1.aboutHouse(); print(house1.rooms); house1.rooms = 10; house1.aboutHouse(); } class House { // private variable int _rooms; int _floors; House(this._rooms, this._floors); void aboutHouse(){ print('This house has $_floors floors and $_rooms rooms.'); } int get rooms{ return this._rooms; } set rooms(int room){ this._rooms = room; } } -------------..
[Dart] Class - 선언 및 생성자 선언 및 생성자 void main() { House house1 = new House(4,2); house1.aboutHouse(); } class House { final int? rooms; final int? floors; House(this.rooms, this.floors); void aboutHouse(){ print('This house has $floors floors and $rooms rooms.'); } } --------------------------------------------[result] This house has 2 floors and 4 rooms. 다른 스타일 생성자 void main() { House house1 = new House(4,2); house1.abou..
[Dart] Null Safety 널을 할당 가능하게 하려면 ? 기호를 추가하면 된다. void main() { int? a; a = null; print('a is $a.'); String? name; name = null; print('you name is $name'); } ----------------------------[result] a is null. you name is null 리스트를 사용하는 경우 '?' 위치 void main() { List aListOfStrings = ['one', 'two', 'three']; // 리스트 멤버가 없는경우는 , 빈 리스트 할당으로 ? 대체 가능 List aNullableListOfStrings = []; // 리스트 멤버가 없는경우는 , 타입 뒤에 ? 위치 List? aNulla..
[Dart] Stream 처리. Stream 기본 코드(await for 구문으로 처리, Listen 대신 사용가능) import 'dart:async'; // You can process a stream using either await for or listen() from the Stream API. Future sumStream(Stream stream) async { var sum = 0; await for (var value in stream) { // 여기서 stream 끝날때까지 loop print('(sumStream)' + sum.toString()); sum += value; } return sum; } // * 는 return type 이 Stream 이라서? 추가확인 필요. Stream countStream(int..
[Dart] Future, async-await 간단한 Future 사용법 정리. import 'dart:async'; void printDailyNewsDigest() { // Future 가 완료되기 이전에 인스턴스만 반환 var newsDigest = gatherNewsReports(); print(newsDigest); } void main() { print('======= Start ======='); printDailyNewsDigest(); // 아래 함수들은 모두 기다리게 하려고 의도함, 하지만 실패 printWinningLotteryNumbers(); printWeatherForecast(); printBaseballScore(); } void printWinningLotteryNumbers() { print('printWinningL..
[Dart] 상속과 변수 초기화 부모/자식 클래스의 변수초기하는 방법 void main() { ECar tesla = ECar(7,6,5,4); GCar sonata = GCar(1,10,3); tesla.seat = 10; sonata.seat = 4; print('tesla: ' + tesla.currentSpeed(300).toString()); print('sonata: ' + sonata.currentSpeed(250).toString()); print('tesla : seat(${tesla.seat}), door(${tesla.door}), power(${tesla.power}), capacity(${tesla.capacity}), '); print('sonata : seat(${sonata.seat}), door(${son..