인스턴스로 생성된 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
'Flutter' 카테고리의 다른 글
[Flutter / GetX] binding 사용하기 (0) | 2022.03.09 |
---|---|
[Flutter / GetX] GetxService를 상속받기 (0) | 2022.03.09 |
[Flutter / 일반] enum을 활용한 분기 (0) | 2022.03.08 |
[Flutter / 일반] API의 json 활용하기; Factory, fromJson, toJson (0) | 2022.03.08 |
[Flutter / 일반] 클래스 객체에 update / addAll 사용하기 (0) | 2022.03.08 |