[Flutter] GetX 컨트롤러 관리 관련 간단 정리

Flutter|2021. 11. 16. 15:54

1. 생성

Get.put(dependency, {tag, permanent = false, builder});
Get.lazyPut(builder, {tag, fenix = false});
Get.putAsync(builder, {tag, permanent = false});

가장 자주 쓰는 것은 put. 

 

2. 삭제

Get.delete<S>({tag, force = false});

delete의 경우, Future<bool>을 return한다.

 

3. 접근

Get.find<S>(). ...

위와 같은 형태로 S 안에 있는 함수와 변수에 접근할 수 있다.

 

4. 줄여쓰기

class Controller extends GetxController {
  static Controller get to => Get.find();
  static Future<bool> get del => Get.delete<Controller>();
}

위와 같은 형태로 작성하면, Get.find<Controller>(). ...는 Controller.to. ...로, Get.delete<Controller>();는 Get.del;로 사용할 수 있다.

GetX package에서 get/lib/get_instance/src/extension_instance.dart를 보면 아래와 같이 나와있으므로 참고함.

/// Finds a Instance of the required Class `<S>`(or [tag])
/// In the case of using `Get.create()`, it will generate an Instance
/// each time you call `Get.find()`.
S find<S>({String? tag}) => GetInstance().find<S>(tag: tag);

/// Deletes the `Instance<S>`, cleaning the memory and closes any open
/// controllers (`DisposableInterface`).
///
/// - [tag] Optional "tag" used to register the Instance
/// - [force] Will delete an Instance even if marked as `permanent`.
Future<bool> delete<S>({String? tag, bool force = false}) async =>
    GetInstance().delete<S>(tag: tag, force: force);

댓글()