플러터의 바뀐 리스트, Null Safety


플러터가 업그레이드 되면서, 
널세이프티 가 적용되었다.
Null 값을 가지는것과 
Null 값을 가지면 안되는것을 
구분해 둔것.

기존엔 모든 변수의 Null 을 체크했지만, 
Null 값을 가지는 nullable 만 체크하도록해, 
속도를 향상시켰다고 한다.

그래서 Null 값을 가지면 안되는 
List 는 Null Safety 가 되면서, 
코드를 쓰는 방식이 바꼈다.

//old
List<foo> fooList = List();
List<foo> fooList = new List<foo>();

//new
List<foo> fooList = <foo>[];
List<foo> fooList = [];
fooList = [];

int fooLength = 5;
string fooText = 'hello';
FooClass fooFunction(int idx) => FooClass(id: idx);

List<FooClass> fooList = List<FooClass>.empty(growable: true);
List<String> fooList =  List<String>.filled(fooLength ,fooText , growable: true);
List<FooClass> fooList = List.generate(fooLength, fooFunction, growable: true);



예전방식으로도 동작은 하지만,
권장하지 않는다.

출처 : https://master-api.flutter.dev/flutter/dart-core/List-class.html

댓글

이 블로그의 인기 게시물

일본어 조사 を(오 : ~을 / ~를)입력 방법

세계의 전기와 전자제품 50Hz / 60Hz , 110V / 220V / 230V