본문 바로가기
Flutter

Flutter lint 2.0.1 대응

by th_bigLight 2022. 5. 26.

Flutter lint 업데이트를 진행함. 최근에 발견해서 업데이트를 진행했다.

 

프로젝트에서 발견된 주요 변화 내용 : 

1. no_leading_underscores_for_local_identifiers

이제 로컬 변수에서 underscore를 쓰지 말라고 한다,, 

BAD!

void print(String _name) {
  var _size = _name.length;
  ...
}

GOOD!

void print(String name) {
  var size = name.length;
  ...
}

하지만 사용하지 않는 변수에 대해서는 지금처럼 underscore 사용을 권장한다.

OK!

[1,2,3].map((_) => print('Hello'));

https://dart-lang.github.io/linter/lints/no_leading_underscores_for_local_identifiers.html

 

no_leading_underscores_for_local_identifiers

no_leading_underscores_for_local_identifiers Group: style Maturity: stable View all Lint Rules Using the Linter DON’T use a leading underscore for identifiers that aren't private. Dart uses a leading underscore in an identifier to mark members and top-le

dart-lang.github.io

 

2. depend_on_referenced_packages

패키지를 import 할 때  pubspec에 depencency를 추가해 주라고 한다.

기존에는 A 패키지를 사용하면 A패키지에서 사용하는 B패키지의 경우 따로 dependency를 추가하지 않고 사용했다면 이제는 B패키지도 동일하게 dependency를 추가해주어야 한다.

https://dart-lang.github.io/linter/lints/depend_on_referenced_packages.html

 

depend_on_referenced_packages

depend_on_referenced_packages Group: pub Maturity: stable View all Lint Rules Using the Linter DO Depend on referenced packages. When importing a package, add a dependency on it to your pubspec. Depending explicitly on packages that you reference ensures t

dart-lang.github.io

 

3. use_build_context_synchronously

BuildContext를 비동기 작업과 함께 사용하지 말라고 한다.

이유 : 나중에 (await 이후에) 사용할 BuildContext를 가지고 있는건 오류가 어디서 발생했는지 찾기 힘들기 때문

BAD!

void onButtonTapped(BuildContext context) async {
  await Future.delayed(const Duration(seconds: 1));
  Navigator.of(context).pop();
}

GOOD!

void onButtonTapped() async {
    await Future.delayed(const Duration(seconds: 1));

    if (!mounted) return;
    Navigator.of(context).pop();
  }

use_build_context_synchronously

 

https://dart-lang.github.io/linter/lints/use_build_context_synchronously.html

 

use_build_context_synchronously

use_build_context_synchronously Group: errors Maturity: experimental View all Lint Rules Using the Linter DO NOT use BuildContext across asynchronous gaps. Storing BuildContext for later usage can easily lead to difficult to diagnose crashes. Asynchronous

dart-lang.github.io

 

'Flutter' 카테고리의 다른 글

cli로 패키지 설치하기  (0) 2022.08.13
빈 위젯을 사용해야 하는 경우  (0) 2022.07.07
Custom Button으로 Drawer 여는법  (3) 2021.08.27
M1에서 Firebase 사용시 오류  (4) 2021.08.22
Positioned widget 가운데 놓기  (0) 2021.07.29

댓글