-
[Dart] 변수 선언 시 var와 그 외의 타입을 지정하는 경우Flutter 2024. 1. 15. 16:44
Dart의 변수 지정 방식은 크게 2가지가 있다.
첫째로
var name = 'dart';
와 같이 var로 지정해주는 방식과
둘째로
String name = 'dart';
와 같이 name의 type을 미리 지정해주고 선언하는 것이다.
물론 var로 해도 dart 컴파일러가 알아서 name이 String 이라는 것을 알고 지정해주기 때문에 상관없다.
그렇다면 언제 var를 쓰고 언제 String같은 타입 지정 방식을 사용하는 것일까?
사실 어떻게 선언하든 코드는 돌아가지만
Dart 공식 스타일가이드의 권장방식은
var는 함수나 메소드 내부의 지역변수를 언선할 때 사용하고
String, double, int, bool 같은 타입 지정은 class 변수나 property를 선언할 때 사용하는 것을 권장한다.
왜 그런지에 대한건 관련 내용을 검색해봤는데 스택오버플로우에서 비슷한 답변을 찾을 수 있었다.
'Flutter' 카테고리의 다른 글
[Flutter] Flutter Hook에서 WebView가 포함된 비동기 함수를 useEffect에서 선언했을 때 나오는 Zone mismatch 에러에 대해서... (0) 2024.09.03 [Flutter] private 변수(_)에 대해서... (0) 2023.12.20 [Flutter] Don't invoke 'print' in production code. 뜨는 이유 (0) 2023.12.18 [Flutter] TextFormField 함수 정리 (0) 2023.11.14 [Dart] extends와 implements의 차이?? (2) 2023.11.02