일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- named parameter
- 유니티최적화
- OPCUA
- c#
- Csharp
- 폴더구성
- 포지션 파라미터
- xml unity
- 4KUnity
- memorymangement
- 다트기초
- Unity3d
- flutter folder
- 유니티
- 네임드 파라미터
- MemoryBarrier
- 싱글톤
- unity icon
- position parameter
- XR
- Unity
- 플러터
- unity 아이콘 깨짐
- optional prameter
- 플러터프로젝트
- Unity 4k 아이콘 깨짐
- 메모리배리어
- C# memorybarrior
- 플러터폴더
- Flutter
- Today
- Total
배 타다 개발자
[Dart] named paramater 와 positional parameter의 차이? 본문
플러터에서 메소드를 쓰다보면 네임드 파라미터와 포지셔널 파라미터라는 용어가 나오는데,
의미를 명확하게 설명하지 못하니 이번 기회에 한 번 짚고 넘어 가보도록 하자.
다트에서 메소드의 파라미터는 리콰이어드 파라미터, 옵셔널 파라미터 크게 두 가지가 있다.
1. Required parameter
전통적으로 써왔던 친숙한 파라미터
findVolume(int length, int breath, int height) {
print('length = $length, breath = $breath, height = $height');
}
findVolume(10,20,30);
출력결과
length = 10, breath = 20, height = 30
유의해서 보아야 하는 파라미터는 옵셔널 파라미터인데
2. Optional parameter (positional, named & default)
이 옵셔널 파라미터가 positional 과 named parameter로 나뉘게 된다.
2.1 optional positional prameter
스퀘어 브라켓 [] 으로 감싸져 있는 파라미터를 포지셔널 파라미터라고한다.
말 그대로 position(위치)가 중요한 파라미터라고 이해하면 기억하기 쉽다.
findVolume(int length, int breath, [int height]) {
print('length = $length, breath = $breath, height = $height');
}
findVolume(10,20,30);//valid
findVolume(10,20);//also valid
출력 결과
length = 10, breath = 20, height = 30
length = 10, breath = 20, height = null // no value passed so height is null
특징은 두 개 이상의 파라미터를 포지셔널 파라미터로 넘길때 파라미터의 순서에 따라 생략 없이 모두 값을
넣어 주여야한다. 따라서 함수를 작성하는데 모든 파라미터를 기입해주어야 하는 압력이 있지만 아래와
같이 api를 만들어 줄 때는 더 읽기 쉽고 명확한 함수히다.
getHttpUrl(String server, String path, [int port=80, int numRetries=3]) {
// ...
}
getHttpUrl('example.com', '/index.html');
getHttpUrl('example.com', '/index.html', 8080);
getHttpUrl('example.com', '/index.html', 8080, 5);
2.2.1 Optional Named Parameter
findVolume(int length, int breath, {int height}) {
print('length = $length, breath = $breath, height = $height');
}
findVolume(10,20,height:30);//valid & we can see the parameter name is mentioned here.
findVolume(10,20);//also valid
- 컬리브라켓 {} 으로 감싸져 있다.
- 컬리 브라켓 안에 있는 값은 옵셔널이다.
- 값을 할당하려면 콜론(:) 을 꼭 붙여야 한다.
- 컬리 브라켓 안에 있는 파라미터의 순서는 상관이 없다.
- 함수에 많은 파라미터를 넘겨줄 때 혼란을 피할 수 있다.
- 함수 작성시에 순서보라는 파라미터의 이름을 참조하기 때문에 좀 더 유연한 편이다.
출력 결과
length = 10, breath = 20, height = 30
length = 10, breath = 20, height = null
2.2.2 Optional Default Parameter(옵셔널 디폴트 파라미터)
findVolume(int length, int breath, {int height=10}) {
print('length = $length, breath = $breath, height = $height');
}
findVolume(10,20,height:30);//valid
findVolume(10,20);//valid
- 옵셔널 네임드 파라미터와 비슷하지만 추가적으로 기본값을 미리 지정하고
추후에 아무런 값도 넘겨 받지 않은 경우에는 기본값이 그 파라미터의 값에 할당 된다.
length = 10, breath = 20, height = 30
length = 10, breath = 20, height = 10 // default value 10 is taken
What is the difference between named and positional parameters in Dart?
Dart supports both named optional parameters and positional optional parameters. What are the differences between the two? Also, how can you tell if an optional parameter was actually specified?
stackoverflow.com
'Graphics > Flutter' 카테고리의 다른 글
플러터 프로젝트 폴더 구성 (0) | 2022.02.26 |
---|---|
[Flutter]ScaffoldMessenger가 Scaffold의 Context 바로 접근 가능 (0) | 2021.12.28 |
[Flutter] 가운데 정렬하기 (0) | 2021.12.14 |
맥에서 플러터 설치 / 폴더 구성 / 생명주기 (0) | 2021.12.09 |
플러터로 안드로이드/IOS 앱 동시에 만들기 (0) | 2021.10.30 |