📱Flutter

Flutter 의 annotation 에 대해

흐성진 2025. 2. 12. 00:28
반응형

오늘은 @override 같이 자주 사용하지만 정확하게 어떻게 작동하는지 모르고 넘어가는 annotation 에 대해서 알아보려고 한다.

 

Annotation 이란?

사전적 의미로는 주석 이라는 뜻을 가지지만

플러터에서 annotation 은 특별할 의미, 기능을 수행한다.

프로그램 코드의 일부가 아닌 프로그램에 관한 데이터를 제공하고, 코드에 정보를 추가하는 정형화된 방법이다.

annotation 을 사용하면 코드가 깔끔해지고 재사용이 가능해진다.

  • 컴파일러에게 코드 검증 정보 제공
  • 개발 도구 빌드/배포 관련 정보 제공
  • 런타임에 특정 기능 실행을 위한 정보 제공

 

Annotation의 종료

@override

https://api.flutter.dev/flutter/dart-core/override-constant.html

 

override constant - dart:core library - Dart API

override top-level constant Object const override Annotation on instance members which override an interface member. Annotations have no effect on the meaning of a Dart program. This annotation is recognized by the Dart analyzer, and it allows the analyzer

api.flutter.dev

 

일반적으로 제일 많이 사용하는 annotation 종류 중에 하나일 것이다.

  • 부모 클래스나 인터페이스의 메서드를 재정의 할 때 사용
  • 컴파일러에게 의도적으로 재정의 했음을 알림
class Parent {
  void sayHello() {
    print("Hello World");
  }
}

class Child extends Parent {
  @override
  void sayHello() {
    print("Hello World from Child");
  }
}

void main() {
  Parent parent = Parent();
  parent.sayHello();

  Child child = Child();
  child.sayHello();
}

 

@override 는 Dart 언어에서 컴파일 타입에 오류를 방지하는데 큰 도움을 준다.

예를 들어 부모 클래스의 메서드 시그니처를 잘 못 입력했을때 @override가 없으면 단순히 새로운 메서드로 인식되지만 @override를 사용하면 컴파일러가 오류를 경고해준다.

 

@deprecated

https://api.flutter.dev/flutter/dart-core/deprecated-constant.html

 

deprecated constant - dart:core library - Dart API

deprecated top-level constant Deprecated const deprecated Marks a feature as Deprecated until the next release. Implementation const Deprecated deprecated = Deprecated("next release");

api.flutter.dev

 

플러터가 업데이트될때마다 생기지 않기를 기도하는 부분이다.

  • 더 이상 사용되지 않는 API를 표시한다.
  • 해당 코드를 사용하는 경우 컴파일러가 경고 메시지를 출력하고 대체 가능한 API 를 안내할수 있다.

 

@deprecated annotation을 사용하면 다음과 같이 나타나게 된다.

 

@immutable

https://api.flutter.dev/flutter/meta/immutable-constant.html

 

immutable constant - meta library - Dart API

immutable top-level constant Immutable const immutable Used to annotate a class C. Indicates that C and all subtypes of C must be immutable. A class is immutable if all of the instance fields of the class, whether defined directly or inherited, are final.

api.flutter.dev

 

  • 개체 불변성을 보장
  • 클래스가 immutable임을 나타내고, 모든 필드를 final 로 설정해야 한다.
  • 하위 클래스도 불변성 준수 필요

 

다음과 같이 처음에 1의 값을 넣고 value 값을 바꾸려고 했을때 오류가 발생한다.

이전에 설명한 개체 불변성을 깨려고해서 발생하는 오류다.

 

@required - 미사용

Dart 2.12 이후 현재는 사용되지 않으며 Named parameters 상의 required 로 변경되었다고 한다.

https://api.flutter.dev/flutter/meta/required-constant.html

 

required constant - meta library - Dart API

required top-level constant Required const required Used to annotate a named parameter p in a method or function f. Indicates that every invocation of f must include an argument corresponding to p, despite the fact that p would otherwise be an optional par

api.flutter.dev

https://dart.dev/language/functions#named-parameters

 

Functions

Everything about functions in Dart.

dart.dev

 

@protected

https://api.flutter.dev/flutter/meta/protected-constant.html

 

protected constant - meta library - Dart API

protected top-level constant _Protected const protected Used to annotate an instance member in a class or mixin which is meant to be visible only within the declaring library, and to other instance members of the class or mixin, and their subtypes. If the

api.flutter.dev

 

  • 해당 인스턴스 멤버가 해당 클래스나 믹스인의 인스턴스와 하위 타입에서만 접근 가능
  • 즉 서브클래스에서만 사용되도록 의도된 메서드나 필드를 문서화할때 사용

 

@factory

https://api.flutter.dev/flutter/meta/factory-constant.html

 

factory constant - meta library - Dart API

factory top-level constant _Factory const factory Used to annotate an instance or static method m. Indicates that m must either be abstract or must return a newly allocated object or null. In addition, every method that either implements or overrides m is

api.flutter.dev

 

  • 해당 인스턴스나 스태틱 메서드가 반드시 새로운 객체를 반환하거나 null을 반환해야함

 

Custom Annotaton

직접적으로 annotation에 대해서도 정의할 수 있다.

Dart 에서는 annotation이 단순히 클래스를 통해서 구현된다.

 

 

 

기본적으로 구현되어있고 자연스럽게 사용하는것들도 한번 알고 사용하면 더 좋을것 같아서 정리해보았다.

반응형