본문 바로가기
Flutter

[Flutter / GetX] GetView를 상속받는 이유

by hymndaniel 2022. 3. 8.

인스턴스로 생성된 controller는 사용하고 싶은 위젯에서 Get.find()로 접근해서 사용할 수 있다. 

Get.find<Controller>();

 

controller에서 static으로 전역으로 간단하게 접근하게 만들 수 도 있다. 

# controller에서
static Controller get to => Get.find();

# 사용하려는 위젯에서
Controller.to.value;

그런데 만약 하나의 위젯에서 하나의 controller만 사용한다면 GetView를 고려할 수 있다. 

Get.find가 필요한 class(위젯)에 StatelessWidget대신 GetView를 상속하고 class에서 사용할 controller를 지정해주면, 클래스 내에서 곧바로 controller로 접근할 수 있다.

// 원래
class Home extends StatelessWidget {

// GetView 사용
class Home extends GetView<Controller> {

 

// GetView 처리 전
Get.find<Controller>().increase();
 
// GetView 처리 후
 controller.increase();

 

출처

https://velog.io/@giyeon/flutter-2-1-Getx-%EB%8B%A8%EC%88%9C-%EC%83%81%ED%83%9C%EA%B4%80%EB%A6%AC

 

 

 
728x90