본문 바로가기

Flutter/Dart

(4)
Dart- 클래스 class Player { final String name; String team; int xp, age; // named constructor 1 Player({ // Dart에서 권장되는 Key:Value 쌍을 이용한 named constructor required this.name, required this.xp, required this.team, required this.age,}); // named constructor 2 Player.createBluePlayer({ required String name, required int age, }) : this.age = age, this.name = name, this.team = 'blue', this.xp = 0; // named constr..
Dart- 함수 함수 선언 - positional parameter 함수의 파라미터 위치에 알맞게 값을 순서대로 넣는 것이다. - named parameter 함수 사용시 들어갈 파라미터값을 명시하는 방법은 함수 선언시 파라미터들에 { }를 써서 Map처럼 선언하는 것이다. 또한 아무런 조치 없이 { }로 감싼 경우 들어가는 파라미터들은 null safety에 의해서 오류를 반환한다. String sayHello({ String name, // null safety에 의한 에러발생 int age, String country, }) { return "hello my name is $name, my age is $age and i lived in $country"; } 이를 해결하기 위해서 아래의 두가지 방식을 사용할 수 ..
Dart- 데이터 타입 정리 List list의 선언은 일반적인 다른 언어들과 동일하다. numbers는 List 형으로 선언되었고 내부에는 collection if 문법이 사용되었다. collection if List intList = [ if(true) 1, ] collection if는 위처럼 collection 내부에서 if 문을 사용하는 기능이다. 이 경우, if 내부 값이 true이므로 1이 intList의 내부값으로 들어간다. String interpolation 문자열 내부에 변수 값을 넣고 싶으면 다음과 같이 $를 사용한다. void main() { var name = "철수"; var greeting = "hello nice to meet you, my name is $name"; print(greeting); /..
Dart- 변수 정리 Nullable Variables Dart의 모든 변수는 기본적으로 nullable하지 않다. 만일 해당 변수에 null을 넣을 수 있도록 설정하고 싶다면 타입 뒤에 ?를 붙이면 된다. nullable 한 변수는 사용할 때마다 null 여부를 체크해야한다. if문을 사용해서 null 여부를 체크할 수도 있고, "변수명?"를 이용해서 체크도 가능하다. Final Variables final 키워드를 통해서 값을 재할당 하지 못하는 변수를 선언을 할 수 있다. Late variables late 키워드를 사용하면 final 변수가 초기값 없이 변수를 선언 할 수 있도록 해준다. 이는 클래스의 final 필드를 나중에 초기화 하는 경우에 사용한다. 즉, laste 키워드는 해당 변수에 어떠한 값이 올 지 모른..