await_only_futures

await_only_futures

Future가 아닌 값에 await를 쓰면 애매한 동작이 되기 쉬워요. await_only_futures 린트 규칙이 어떤 경우에만 await를 허용하는지 알아볼게요.

출처: await_only_futures

본문

상태: 안정(stable) · 핵심 규칙(Core) · 자동 수정 가능(Fix available)

Future가 아닌 것에 await를 쓰지 마세요. await는 다음 타입들에만 사용할 수 있어요.

  • Future
  • FutureOr
  • Future?
  • FutureOr?
  • dynamic

추가로, await null은 마이크로태스크(microtask) 지연을 만들기 위한 용도로 특별히 허용돼요.

나쁜 예

main() async {
  print(await 23);
}

좋은 예

지연을 의도한 경우라면 await null로 명시적으로 표현하고, 아니라면 그냥 값을 사용하면 돼요.

main() async {
  await null; // If a delay is really intended.
  print(23);
}

규칙 활성화하기

analysis_options.yaml 파일의 linter > rules 아래에 await_only_futures을 추가하면 돼요.

linter:
  rules:
    - await_only_futures

룰을 YAML 맵 문법으로 설정하고 있다면 linter > rules 아래에 await_only_futures: true를 넣어 주세요.

linter:
  rules:
    await_only_futures: true

더 알아보기